How To Manage Dotfiles

How To Manage Dotfiles

Introduction

When your OS crashes or you need to change it, you will reconfigure .dotfiles such as .zsh, .gitconfig and .vimrc. These .dotfiles change over time as you start customizing Linux according to your needs. How do we save this configuration to avoid manual work every time? The answer is Dotfiles Manager.

How to build a Dotfile Manager

There are a lot of Dotfile Managers out there, for example, Atlassian, yam, and more here. I prefer the scratch way.

First, you should Create a new repository on GitHub named .dotfiles and clone it in the $HOME directory where you will host your dotfiles. Clone your repo to the .dotfiles directory

SSH

git clone git@github.com:<YOUR_GITHUB_USERNAME>/.dotfiles.git ~/.dotfiles

HTTPS

git clone https://github.com/<YOUR_GITHUB_USERNAME>/.dotfiles.git ~/.dotfiles

Now, you have an empty directory. The next step is to add your files in it. Move your files to ~/.dotfiles.

Dotfiles Dir

For simplicity, we will work on the .tmux.conf file then you can do this for the rest of the files.

Now, we will move .tmux.conf to ~/.dotfiles directory. You can use mv to move file to .dotfiles, for example

mv .tmux.conf .dotfiles/

mv

With the command ll -al ~/.dotfiles | grep tmux we search for any files or directories containing tmux name in ~/.dtofiles but not found. After run mv .tmux.conf .dotfiles and search again .tmux.conf file moved to .dotfiles directory successfully.

BUT

The Tmux configuration has broken. Of course, broken because there is no configuration file in the $HOME directory were in, it’s in the ~/.dotfiles directory. How do we use this file and at the same time keep it tracked by git in ~/.dotfiles?

We use ln util to link files from ~/.dotfiles the directory to the same file in the $HOME directory where tmux can use it and track all changes.

ln -s ~/.dotfiles/.tmux.conf ~/.tmux.conf

With the command above we created a copy of the .tmux.conf file to the $HOME directory with a link as shown below

Link betweem files

Everything is ready. By opening ~/dotfiles/.tmux.conf and ~/.tmux.conf side-by-side, every editing happens to any of two files, it happens to another.

Tracking

we have a file that configures tmux and same file tracked by git into your .dotfiles GitHub repo.

Finally, go to ~/.dotfiles and push to Github.

Here is my repo with the code.

Summary

Let's summarize some important things we have done.

  1. Create repo named .dotfiles

  2. Clone it into your Home dir

git clone git@github.com:<YOUR_GITHUB_USERNAME>/.dotfiles.git ~/.dotfiles
  1. Move your files to ~/.dotfiles using mv

  2. Link your moved files with ln and it will automatically be copied for you

  3. Push your files to GitHub