Andrei E.P. Ciobanu

A clear way of structuring your dotfiles

Last updated on

What is a dotfile

In short terms: the programs that you are using are generally supporting configuration their behaviour in some files where you add some specific lines for each program and those files are generally hidden (in unix a hidden file has a .(dot) prefix).

I will not get too much into details because Google can provide a comprehensive response to that is no use to add here too.

Common configurations

Some of the most common configurations are exports and aliases.

And some of those are specific to some specific programs and some environments (eg: a home environment or a work environment, where you might use different toolboxes).

A structure that really works for me

Depending on the shell interpreter used, you can have different root configuration files:

  • Bash -> .bashrc
  • Zsh -> .zshrc

Once you open a new terminal session, the corresponding file content is executed, meaning that this is the entry point of your configurations.

Structure

Just create a common_system file with the following content:

[ -f ~/.common_exports ] && . ~/.common_exports
[ -f ~/.exports ] && . ~/.exports
[ -f ~/.common_aliases ] && . ~/.common_aliases
[ -f ~/.aliases ] && . ~/.aliases

And you can think of these as follows:

  • common_exports: a list of export statements that are commonly used by any of your environments.
    Generally contains modifications of the $PATH env var in order to point to other tools that you need.
  • exports: a list of exports that are specific to your environment (home or work). These can also override exports done in common_exports.
  • common_aliases: a list of alias statements that are commonly used by any of your environments Generally contains aliases for different tools that you are using in a unix environment (ls, cd, etc)
  • aliases: a list of aliases that are specific to your environment (home or work). These can also override exports done in common_aliases.

Just source common_system in your shell interpreter configuration file and everything is executed every time you open a new session:

[ -f ~/.common_system ] && . ~/.common_system

And how are those exports and aliases files specific to environment

Once you have a repository containing your dotfiles, you can structure those as follows:

~/.dotfiles
├── common
│   ├── aliases
│   ├── exports
│   └── system
├── home
│   ├── aliases
│   └── exports
└── work
    ├── aliases
    └── exports

Where you can symlink:

  • common/aliases to ~/.common_aliases
  • common/exports to ~/.common_exports
  • common/system to ~/.common_system

And based on your environment that you are setting up you will symlink the home or work aliases and exports files in your home directory.

In the end you need to get to a structure similar to this:

~/
├── .aliases -> ~/.dotfiles/work/aliases
├── .common_aliases -> ~/.dotfiles/common/common_aliases
├── .common_exports -> ~/.dotfiles/common/common_exports
├── .common_system -> ~/.dotfiles/common/common_system
├── .exports -> ~/.dotfiles/work/exports
├── .zshrc -> ~/.dotfiles/common/zshrc

In the end

This is the way that I made it work for me. I am not saying that it’s perfect but since I came with this, I encountered no issue with maintaining and adding more needed configurations so feel free to give it a shot.

For a detailed view on my dotfiles, you can take a look here.