Managing dotfiles: Feel at home on every machine

Fré Dumazy
3 min readDec 24, 2018

--

Just an image for the preview of this article — by Fabricio Marques

If you’re a developer, you probably already had to work on multiple machines at the same time. This could be a Mac at the office, some servers you have to connect to, another laptop at home, etc. If you use the shell a lot, you might have your own configuration with aliases or certain preferences. These configurations are mostly kept in ‘dotfiles’. They are named that because they are hidden files starting with a dot, for example .bash_profile, .gitconfig, .yarnrc, etc. They can mostly be found in your home directory.

When you’re switching to another machine, you might want to use those same aliases you had on your other setup as well. At first, you might just copy-paste your .gitconfig from your home directory on one machine over to the other. This works quite well. But if you have multiple configuration files, this might get annoying to set all of those up on the other machine.

The worst thing is when you want to make a change, for example, add a new alias. Then you have to go back to all your machines to make the change there as well.

Creating a dotfiles repository

So how can we make this a lot easier? There are several ways to organise and manage these dotfiles, but the easiest in my case was to use Git for this. I have a MacBook at the office, several Debian-based servers to connect to at work and at home, one laptop running Ubuntu and another one running Linux Mint. On all of these, I use Bash as my preferred shell. They also all have Git installed.

The advantage of using a versioning system for this, is that it’s easy to copy your changes to the other machines. As an extra, you can go back to a previous version in case you broke the configuration files, or you can open source it and share your awesome preferences and handy aliases with other developers.

To get started, create a new git repository with git init and add a file named .bash_profile to it.

# Make tab completion case insensitive
bind "set completion-ignore-case on"
# Aliases for easy navigation
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."

Awesome, we have our first dotfile. We now could just simply clone our repository in the user’s home directory on every machine, but this would make it messy and could cause accidental file additions to our repository. A better way for this is to create a script that uses symlinks instead.

Create a file in the repository called init.sh and make sure it’s executable by running chmod +x init.sh.

#! /bin/bash# get the absolute path of our dotfiles repository
DIR=$(cd `dirname $0` && pwd)
# overwrite existing symlinks if necessary
ln -sf $DIR/.bash_profile ~/.bash_profile
# apply the new .bash_profile
. ~/.bash_profile

Now we can clone our repository wherever we want, for example in ~/dotfiles/, and run init.sh to create the symlink. Our case-insenstive tab completion and navigation aliases are now available on the new machine.

Machine-specific configurations

There might be some configuration or aliases that are specific to the machine you’re working on. We don’t want these things in every .bash_profile file on every machine. A way to do this, is to keep those in .bash_profile.local files. Just these three lines to the .bash_profile in the repository.

# include local bash_profile if exists
if [ -f ~/.bash_profile.local ]; then
. ~/.bash_profile.local
fi

We can now add the machine-specific things in the file ~/.bash_profile.local without having to worry that they will get copied over to all other machines.

With this repository, we can add other configuration dotfiles such as .gitconfig or .vimrc in the same way. We can add more things to it and update the init script to set everything up in a single command.

Feel free to browse or fork my dotfiles repository template here. All suggestions are welcome :)

--

--