Managing dotfiles with Stow
Generally speaking, dotfiles are files whose file names start with a dot (.). They are hidden files in Linux and Unix-like operating systems. Developers and sysadmins commonly use the term “dotfiles” specifically for the subset of files that are relevant for configuring the applications and utilities that we care about.
If you use a version control system like Git, an editor like Vim, or a shell, you most likely configure them in their respective dotfiles like ~/.git
, ~/.vimrc
, or ~/.zshrc
.
As with almost anything, we typically want these files to be under some version control system so that we can share them between multiple computers or revert them to a previously working configuration. There are multiple ways to achieve that, but my favorite is to keep all dotfiles together in one folder.
Dotfiles are typically scattered in a home directory (~/
) between other files and folders. However, we can keep them separated in a single location, like a Git repository, and link them to the original locations with symlinks. Symlinks, or symbolic links, are special Linux/Unix files that point to other files or folders. Creating them manually for all our configuration files is tiresome, but there is an excellent utility called Stow that can do it for us. It describes itself as a symlink manager:
Stow is a symlink farm manager which takes distinct sets of software and/or data located in separate directories on the filesystem, and makes them all appear to be installed in a single directory tree.
Please note that if you are not able to run Stow on your operating system like Windows, you might need to look for alternative utilities such as dploy. However, the same principles will apply.
To use Stow and Git to manage dotfiles we need to follow a couple of steps:
Move respective dotfiles to a new directory, mine is
~/dotfiles
. The folder should be placed inside the home directory because Stow will use the parent folder as the default target location. The structure inside it should mimic the structure of the original locations, so if a config file was in~/subfolder/.conf
, it will become~/dotfiles/subfolder/.conf
.Make that directory a Git repository with
git init
and ideally use a remote repository to store a copy outside of your computer, e.g. on GitHub.Run
stow .
(note the dot denoting the current directory) inside your new dotfiles directory, e.g. inside~/dotfiles
. The command will create all your symlinks.
Now you should have a single location for your config files, have them under version control, and have symbolic links created for you that point from the original location to your new folder.
Never lose a config again,
Petr