自如wi-fi管理密码_如何使您的Dotfile管理轻松自如

本文详细介绍了如何优雅地管理和同步点文件,包括使用rsync和符号链接,创建同步和引导脚本,拆分.bash_profile以及添加自定义支持。通过将点文件组织在git仓库中,您可以方便地备份、分享和个性化您的开发环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自如wi-fi管理密码

by Mohammed Ajmal Siddiqui

由Mohammed Ajmal Siddiqui

如何使您的Dotfile管理轻松自如 (How to make your Dotfile management a painless affair)

In the first article, I introduced dotfiles. In this one, we’ll cover their development and management.

第一篇文章中 ,我介绍了dotfiles。 在这一节中,我们将介绍它们的开发和管理。

Note: This is the second article in the series and discusses more advanced aspects of dotfile management. To learn about what dotfiles are and the very fundamentals of dotfile management, read the first article.

注意:这是该系列的第二篇文章,并讨论了点文件管理的更多高级方面。 要了解什么是点文件以及点文件管理的基础知识,请阅读第一篇文章

In the last article, we added a few aliases and functions to the .bash_profile and the .bashrc file. We also learned that those aren’t the only dotfiles available for us to customize.

在上一篇文章中,我们向.bash_profile.bashrc文件添加了一些别名和函数。 我们还了解到,这些并不是我们可以定制的仅有的点文件。

This article focuses on making our approach to dotfile management more sophisticated and scalable. At this point, there is one important thing you need to bear in mind from this point onwards. Dotfiles are a matter of personal preference, and so is their management. You should manage them your way. This article only provides general guidelines and common ways to deal with recurring tasks in dotfile management.

本文着重于使我们的点文件管理方法更加复杂和可扩展。 从这一点开始,您需要牢记一件事。 点文件是个人喜好问题,其管理也是如此。 您应该按照自己的方式进行管理。 本文仅提供处理点文件管理中的重复任务的一般准则和通用方法。

设置环境 (Setting Up Your Environment)

Start with creating a directory for your dotfiles and cd into it. I like to have mine in the Projects folder in my home directory, but this is up to you:

首先为您的dotfile创建目录并cd进入该目录。 我喜欢在我的主目录的Projects文件夹中找到我的文件夹,但这取决于您:

$ mkdir ~/Projects/dotfiles$ cd ~/Projects/dotfiles

This is where we’ll have all our dotfiles. Let’s start by making this a git repository.

这是我们所有点文件的地方。 让我们首先将其设为git存储库。

$ git init

Let’s start by moving the .bash_profile from the HOME directory to our new dotfiles directory.

首先,将.bash_profileHOME目录移动到新的dotfiles目录。

$ mv ~/.bash_profile ~/Projects/dotfiles/.bash_profile

Let’s commit this file.

让我们提交这个文件。

$ git commit -am "Added .bash_profile"

And there you have it! This is exactly how you’d work on any other project, and that’s exactly how you should manage your dotfiles.

在那里,您拥有了! 这就是您在任何其他项目上的工作方式,也是您管理点文件的方式。

Why version control, one might ask. People love committing their dotfiles to version control for a couple of reasons:

有人可能会问为什么要进行版本控制。 人们喜欢将其dotfile提交到版本控制有两个原因:

  • Pushing the dotfiles to a remote repo allows people to share their dotfiles with others or access them remotely when they need them. This is also a secure way to back your dotfiles up.

    将点文件推送到远程存储库中,使人们可以与他人共享其点文件或在需要时远程访问它们。 这也是备份点文件的安全方法。
  • Version control allows you to see how your dotfiles evolve over time.

    版本控制使您可以查看点文件随着时间的变化。

But if you start another terminal instance, you’ll notice that your setup is broken! The terminal doesn’t source your .bash_profile or .bashrc from a custom folder, as these files are expected to be found in the home directory.

但是,如果您启动另一个终端实例,则会注意到您的设置已损坏! 终端不会从自定义文件夹中获取.bash_profile.bashrc ,因为这些文件可能会在主目录中找到。

So we need a way to keep our dotfiles in the dotfiles directory in sync with our home directory. You can do this in any way you want, even if it is as simple as copying all the files in your dotfiles directory to the home directory with a script. But there are more elegant approaches. Let’s look at two of them.

因此,我们需要一种使dotfiles目录中的dotfiles与主目录保持同步的方法。 即使使用脚本将dotfiles目录中的所有文件复制到主目录一样简单,您也可以按照自己想要的方式进行操作。 但是还有更优雅的方法。 让我们看看其中两个。

rsync方法 (The rsync Approach)

One way to deal with the problem of having your dotfiles in a directory other than the home directory is to copy the files using a script. But there is a far better way to do this than using the cp command: the rsync command.

解决将dotfiles存放在主目录以外的目录中的一种方法是使用脚本复制文件。 但是有比使用cp命令更好的方法: rsync命令。

Rsync, which stands for “remote sync”, is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed. Thus, this approach is both more efficient and more scalable when it comes to syncing dotfiles. This article covers rsync in more detail.

Rsync代表“远程同步”,是一种远程和本地文件同步工具。 它使用一种算法,通过仅移动已更改的文件部分来最大程度地减少复制的数据量。 因此,在同步点文件时,这种方法既高效又可扩展。 文章涵盖rsync的更多的细节。

Run this command to sync your dotfiles directory with your home directory:

运行以下命令将dotfiles目录与主目录同步:

$ rsync . ~

The command works in exactly the same way as the cp command. It copies the contents of the source (the current directory: .) to the destination (the home directory: ~).

该命令的工作方式与cp命令完全相同。 它将源(当前目录: . )的内容复制到目标(主目录: ~ )。

However, you may have utility scripts in your dotfiles directory which you may not want to copy to the home directory. In this case, you can exclude these files using the --exclude flag. In fact, you would rather not sync the .git directory within your dotfiles folder with your home directory. So here’s the updated command:

但是,您的dotfiles目录中可能包含实用程序脚本,您可能不想将其复制到主目录。 在这种情况下,可以使用--exclude标志排除这些文件。 实际上,您宁愿不将dotfiles文件夹中的.git目录与主目录同步。 所以这是更新的命令:

$ rsync --exclude ".git/" . ~

You can use the --exclude flag multiple times to exclude multiple files. This approach is used by mathiasbynens in his dotfiles.

您可以多次使用--exclude标志来排除多个文件。 Mathiasbynens在其点文件中使用了这种方法。

符号链接 (Symlinking)

Another approach to syncing dotfiles is creating symlinks from the dotfiles in your dotfiles directory to the home directory. If you don’t know what symlinks are, I suggest you read about it here.

同步点文件的另一种方法是创建从点文件目录中的点文件到主目录的符号链接。 如果您不知道什么是符号链接,建议您在这里阅读。

This is the approach I use in my dotfiles, and I do so because of one major advantage over file copying based approaches - autoupdating. The symlinks in the directory are essentially aliases to the original files in your dotfiles directory, so any changes you make are automatically reflected therein. Which means you don’t have to run your sync command/script every time you make a change. This is super useful.

这是我在点文件中使用的方法, 之所以这样做是因为与基于文件复制的方法相比,它的一个主要优势-自动更新。 目录中的符号链接实质上是dotfiles目录中原始文件的别名,因此您所做的任何更改都会自动反映在其中。 这意味着您不必每次进行更改都运行同步命令/脚本。 这是超级有用的。

You can symlink your .bash_profile to the home directory using the ln command with the -s flag (and the -v flag to make it verbose):

您可以使用带有-s标志的ln命令将.bash_profile符号链接到主目录(并使用-v标志使它变得冗长):

$ ln -sv ~/Projects/dotfiles/.bash_profile ~

Now, whenever you save changes you make to your .bash_profile, they’ll automatically be reflected in the home directory, and you can start a new terminal or source .bash_profile to see them in action.

现在,每当您保存对.bash_profile所做的更改时,它们都会自动反映在主目录中,并且您可以启动新的终端或source .bash_profile来查看它们的作用。

创建用于同步和引导的实用程序脚本 (Creating Utility Scripts for Syncing and Bootstrapping)

At this point, you’re executing terminal commands to sync your dotfiles with your home directory. This approach is almost impossible to scale the moment you have more than 2 - 3 files to deal with.

至此,您正在执行终端命令以将点文件与主目录同步。 当您要处理的文件超过2-3个时,几乎无法扩展这种方法。

Hence, it is better to write a couple of utility scripts that will help keep your dotfiles in check. You should have at least one script in your dotfiles repo, the one you use for syncing. Your sync script should essentially use your syncing mechanism to sync your dotfiles with the home directory. It should also have a mechanism to exclude certain files from being synced. Files like the sync script, the bootstrap script, the .git directory, the README.md file, etc. should be excluded.

因此,最好编写一些实用程序脚本,以帮助检查您的点文件。 您的dotfiles存储库中至少应包含一个脚本,该脚本用于同步。 同步脚本实际上应使用同步机制将点文件与主目录同步。 它还应具有一种机制,可以从同步中排除某些文件。 应排除同步脚本,引导脚本, .git目录,README.md文件等文件。

Currently, I have a sync function in my bootstrap.exclude.sh script that handles syncing and excludes any files that have a .exclude in the file name, in addition to the ones stated above. This is a fairly fail-safe mechanism. You can check it out here.

目前,我的bootstrap.exclude.sh脚本中具有sync功能,该功能可处理同步,并排除上述文件名之外的文件名中带有.exclude任何文件。 这是一种相当安全的机制。 您可以在这里查看

It is highly recommended that you have another script to bootstrap a new system with your dotfiles and setup your development environment.

强烈建议您使用另一个脚本来使用点文件引导新系统并设置开发环境。

One important thing you can use your bootstrap script for is installing packages and tools that you commonly use.

可以将引导脚本用于其中的一件事是安装常用的软件包和工具。

For example, I am a Node.js developer and I use a Mac, so I can use the homebrew package manager to install tools and utilities that I usually use. I can include something like this in my bootstrap script:

例如,我是一个Node.js开发人员,并且使用Mac,因此可以使用自制软件包管理器来安装我通常使用的工具和实用程序。 我可以在引导脚本中包含以下内容:

# Make sure we’re using the latest Homebrew
brew update
# Upgrade any already-installed formulae
brew upgrade
# NodeJS
brew install node
# Heroku
brew install heroku
# Yarn - an alternative to npm
brew install yarn
# Docker for containerization
brew install docker

This script installs Node, Heroku, yarn and Docker on my Mac. Say I end up formatting my Mac or buying a new one. I don’t have to install all the things I use manually. Instead, I can clone my dotfiles from my remote repository and run the bootstrap script, which sets everything up for me. Since you may have a lot of things you use, it is best to separate this out into its own file. Check my brew.exclude.sh file out for an example.

该脚本在Mac上安装Node,Heroku,Yarn和Docker。 假设我最终格式化了Mac或购买了新Mac。 我不必安装我手动使用的所有东西。 相反,我可以从远程存储库克隆我的dotfiles并运行引导脚本,该脚本为我完成了所有设置。 由于您可能要使用很多东西,因此最好将其分离到自己的文件中。 查看我的brew.exclude.sh文件以获取示例。

Your bootstrap script should handle these things:

您的引导脚本应处理以下问题:

  1. Making any relevant directories that you use (for example the ~/Projects directory).

    制作您使用的任何相关目录(例如~/Projects目录)。

  2. Call your sync script to sync your dotfiles with the home directory.

    调用同步脚本将点文件与主目录同步。
  3. Install all the tools, utilities, languages, etc. that you commonly use.

    安装所有常用的工具,实用程序,语言等。

My bootstrap script is pretty minimal and handles all these things, so that might be a good place to start.

我的引导脚本非常少,可以处理所有这些事情,因此这可能是一个不错的起点。

拆分您的.bash_profile (Splitting Up Your .bash_profile)

As we add a lot of aliases and functions, we start to realize that the .bash_profile becomes rather big and cumbersome to manage. Let’s fix this problem.

当我们添加了许多别名和函数时,我们开始意识到.bash_profile变得相当庞大且难以管理。 让我们解决这个问题。

The source command can be used within a script to execute the commands in the file given as an argument to the command. So we can create additional files to hold our functions and aliases and source them into our .bash_profile. It is a common convention to call these files .functions and .aliases respectively.

可以在脚本内使用source命令来执行文件中作为命令参数给出的命令。 因此,我们可以创建其他文件来保存我们的函数和别名,并将其来源到.bash_profile 。 这是调用这些文件共同约定.functions.aliases分别。

Create .functions and .aliases by using this command (while in your dotfiles directory):

创建.functions.aliases使用此命令(而在你点文件目录):

$ touch .functions .aliases

Now cut and paste all the functions from your .bash_profile into .functions and all the aliases into .aliases. Finally, add the following lines to your .bash_profile:

现在削减,并从功能粘贴所有.bash_profile.functions和所有的别名为.aliases 。 最后, .bash_profile添加到.bash_profile

source .functionssource .aliases

This is what’s happening when you open a new terminal window:

这是当您打开新的终端窗口时发生的情况:

  1. The .bash_profile is evaluated.

    .bash_profile被评估。

  2. The source .functions command is executed and thus you can now use your functions.

    source .functions命令已执行,因此您现在可以使用函数了。

  3. The source .aliases command is executed and thus you can now use your aliases.

    source .aliases命令已执行,因此您现在可以使用别名。

You can split the contents of your .bash_profile in any way you please and just source the relevant files. Note that you’ll need the .functions and .aliases files in your home directory, so make sure you sync your dotfiles folder with the home directory for your changes to take effect.

您可以分割你的内容.bash_profile你请任何办法,只是source相关的文件。 请注意,您所需要的.functions.aliases在你的家目录中的文件,所以一定要同步您的点文件文件夹的主目录的更改生效。

Now that we have our dotfile management workflow in place with a dotfiles repository, a mechanism to sync the contents of the repo with the home directory, and the ability to split our code into manageable files, we can happily hack away at our development environment.

现在,我们已经有了一个dotfiles存储库,一个dotfiles存储库,一个将回购内容与主目录同步的机制以及将代码拆分为可管理文件的能力,我们可以很高兴地在我们的开发环境中闲逛了。

While this is sufficient to play with your dotfiles, there is one more important aspect of dotfiles that should be addressed: sharing.

虽然这足以处理您的点文件,但应解决点文件的另一个重要方面:共享。

添加自定义支持 (Adding Support For Customization)

Hosting your dotfiles in a public repository is seldom enough for them to be shareable. If you would like others to experiment with your dotfiles, there are a few things that you should do:

在公共存储库中托管您的dotfile很少足以共享它们。 如果您希望其他人尝试使用您的点文件,则应做一些事情:

  1. Make sure there is some documentation on how to install and use your dotfiles. This usually goes in the README.md of your project. The mechanism you use to exclude files from being synced with the home directory should also exclude this file. You should also take a look at the README files of popular dotfile repos to get a sense of what people put in there. Here’s a link to mine.

    确保有一些有关如何安装和使用您的点文件的文档。 这通常在项目的README.md中。 用于排除文件与主目录同步的机制也应排除此文件。 您还应该查看流行的dotfile存储库的README文件,以了解人们在其中放置了什么。 这是我的链接

  2. If possible, support a mechanism for people to easily customize your dotfiles without having to delve deep into them. This mechanism is completely your choice, though I’ll share mine below.

    如果可能,请支持一种机制,使人们可以轻松自定义您的dotfile,而不必深入研究它们。 尽管我将在下面分享我的观点,但是这种机制完全是您的选择。
Note: The rest of this section discusses my approach to allowing painless customization of my dotfiles. I personally love my approach (and that’s why I use it, duh) but every person has their own way of doing things. I suggest you check out other dotfile repos for inspiration. I’d be happy to hear about your approaches in the comments section.
注意:本节的其余部分讨论了允许我的dotfile轻松定制的方法。 我个人很喜欢我的方法(这就是我使用它的原因,du),但是每个人都有自己的做事方式。 我建议您查看其他dotfile存储库以获取启发。 我很高兴在评论部分听到您的方法。

In order to support customization, all of my main dotfiles end with something like this (you can find an example of my .functions file here).

为了支持自定义,我所有的主要点文件都以这样的结尾(您可以在此处找到我的.functions文件的示例)。

# This should be the last line of the file# For local changes# Don't make edits below this[ -f ".functions.local" ] && source ".functions.local"

Basically, each file named .filename (for example) ends with something like:

基本上,每个名为.filename文件都以类似以下内容的结尾:

[ -f ".filename.local" ] && source ".filename.local"

This command checks to see whether a file with the same filename but an extension of .local exists, and if it does, sources it.

此命令检查是否存在具有相同文件名但扩展名为.local文件,如果存在,则将其作为源。

Since this is the last line of the file, the .filename.local file is the last to be sourced and so all the settings and configurations defined in it persist and can override the ones put in other files.

由于这是文件的最后一行,因此.filename.local文件是最后一个源文件,因此该文件中定义的所有设置和配置都将保留,并且可以覆盖其他文件中的设置和配置。

This allows people experimenting with my dotfiles to customize them without having to modify my code at all! Neat, huh?

这使人们可以尝试使用我的点文件来自定义它们,而无需完全修改我的代码! 整洁吧?

Also, all .local files are ignored in my .gitignore.

另外,我的.gitignore中将忽略所有.local文件。

下一步 (Next Steps)

At this point, you know almost all you need to know about dotfile management, but there are a few things that this and the previous article miss out on:

至此,您几乎几乎了解了所有有关点文件管理的知识,但是此内容和上一篇文章却遗漏了一些内容:

  • Customizing your prompt (this is truly an art and you should invest time in this)

    自定义提示(这确实是一门艺术,您应该为此花费时间)
  • Using a dotfile manager such as dotty (or autodot, which is something I came up with)

    使用点文件管理器,例如dotty(或autodot ,这是我想出的东西)

I’d recommend looking into these things when you can.

我建议您尽可能地研究这些内容。

结论 (Conclusion)

That’s all for this series on dotfile management. I’d love to hear your opinions about this article, and more than that, to see how creative you guys get with your dotfiles. Reach out to me in the comments to tell me how you liked this article and share any other dotfile management tricks you know of. Also, I’d be very happy if you take a minute to gimme feedback on my dotfiles, or suggest improvements using GitHub issues.

这就是本系列有关点文件管理的全部内容。 我很想听听您对本文的看法,更重要的是,希望了解你们的dotfile如何发挥创造力。 在评论中与我联系,告诉我您对本文的看法,并分享您知道的任何其他dotfile管理技巧。 另外,如果您花一点时间给我有关dotfile的反馈意见,或者使用GitHub问题提出改进建议,我将非常高兴。

I loved the idea of dotfiles so much that it inspired me to create a basic dotfile management framework - autodot. The framework is in its infancy, so I’m looking for enthusiastic people who can give me feedback for the framework, contribute to it by telling me about bugs and making feature requests, and contribute to the code and documentation. Do take some time out for this! :)

我非常喜欢点文件的想法,以至于启发了我创建一个基本的点文件管理框架-autodot 。 该框架尚处于起步阶段,因此我正在寻找热心的人,他们可以为我提供有关该框架的反馈,通过告诉我有关错误和提出功能要求的方式为框架做出贡献,并为代码和文档做出贡献。 为此花点时间! :)

ajmalsiddiqui/autodotautodot — A dotfile management system that makes sharing your dotfiles easy while keeping you in the loop.github.com

ajmalsiddiqui / autodot autodot —一个点文件管理系统,可以轻松共享您的点文件,同时保持循环。 github.com

Also, connect with me on GitHub and LinkedIn.

另外,在GitHubLinkedIn上与我联系。

Good luck and Happy Coding! :)

祝您好运,编码愉快! :)

翻译自: https://www.freecodecamp.org/news/dive-into-dotfiles-part-2-6321b4a73608/

自如wi-fi管理密码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值