跳到主要内容

Manage Dotfiles

信息

I've created a dotfiles for multi softwares including multi configurations, see GitHub - ShangjinTang/dotfiles. You can clone it or take it as a dotfiles sample.

Dotfiles are important files that will play an integral role in your career as a software developer.

First, they can help make you more productive. But not only that - you'll be able to have that productive setup you created for youself on any machine.

This article is an introduction on how to get started with dotfiles.

What are dotfiles?

Many computer software programs store their configuration settings in plain, text-based files or directories.

Dotfiles are configuration files for various programs, and they help those programs manage their functionality.

What sets them apart from regular files and directories is their prefix.

Dotfiles are named that way because each file and directory starts with a dot (.)

On Unix based systems, dotfiles are hidden by the Operating System by default.

Examples of common dotfiles Most programs store their configurations in your home directory by default.

Some common dotfiles that you may have heard of or used before are:

  • If you use the Bash shell, you might have a .bash_profile and .bashrc file, both of which contain scripts that load each time you start a new terminal session and configure the shell.
  • If you use the Zsh shell, you would have (or would've created) a .zshrc file which configures and customises the shell.
  • If you use the command line code editor Vim, you would store its configurations in a .vimrc file.
  • After setting up and configuring Git on your local machine, you would have a .gitconfig file, which would contain all your information and settings.
  • Many programs, instead of storing their configurations in your home directory, instead store them in the hidden .config directory (folder) on your system.

Why Use Dotfiles?

Your dotfiles are personal to you.

You spend a sufficient amount of time fine-tuning your setup. You curate configurations and settings that best suit your workflow, aesthetic, and preferences. And you end up with a development environment that helps you, personally, be more productive.

What if after all that time you spent, you now have to switch to a new, different machine? Does that mean you have to start all over again from the beginning?

How would you remember the exact settings and commands you used?

Or what if you have a second machine and you want your set up to be exactly the same on both systems?

One of the main goals of developers is to automate repetitive tasks.

Creating a dotfile repository that is source-controlled and hosted on GitHub will save you time when you want to set up a new computer and install the exact same settings you created for your previous one.

That way all your settings and preferences can be reusable and consistent on other machines.

How to Create Dotfiles

It's good practice to have all your dotfiles in their own folder.

For simplicity's sake, I'll show an example of how to create a folder at the root of your home directory. But you can add the folder wherever is more convenient for you.

Also, I'll be showing examples of how to create a .zshrc and .vimrc file, but similar ideas apply to any other dotfiles you create.

cd ~
mkdir .dotfiles
cp ~/.zshrc ~/.dotfiles/.zshrc ~/.dotfiles/.vimrc

You may have noticed that none of the settings you added to the files in the dotfiles folder have any effect on your system.

A program's configuration files, as mentioned previously, are hidden and stored in the home directory by default. This is where the program will look for and read its settings from.

It's a good idea to symlink (or create a symbolic link -a pointer) the file in the dotfiles directory where you have stored your preferred settings alongside with other files you created, with the file in its default home directory.

It's like the file will be in two places at the same time!

The file will be in both the dotfiles directory and there will also be a 'copy' of it in the home directory.

To create a link, you use the ln (stands for link) command with the -s argument (which stands for symbolic).

Here's how to symlink the .zshrc and .vimrc files:

ln -s ~/dotfiles/.vimrc ~/.vimrc
ln -s ~/dotfiles/.zshrc ~/.zshrc

How to Version Control your Dotfiles

Having your files under version control will help you track all the changes you make to them over time, and will also allow you to share them on GitHub.

Make sure to change directory into the dotfiles directory (cd ~/.dotfiles).

Follow these steps to organise your files in a git repository:

  1. Initialise the repository:

    git init
  2. Add all the files you've created so far:

     git add .
  3. Commit the changes and add a commit message:

    git commit -m "Added dotfiles"

Once git repository is created, you can upload it to GitHub or GitLab so it won't be lost.

危险

Do you create public repository if you want to store some privacy data, such as GitHub token, ssh keys, etc.

信息

These link procedure are done by hand and hard to maintain across different environments; for automatically installation with config files, I recommend to use GitHub - dotbot.