Customize, Simplify, and Automate Your Workspace With Dotfiles

Introduction

Have you already formatted your computer? Or, have you already changed your laptop?

For both of them, you had to visit each software's website to download and install everything manually. Or even worse, you had to set up and customize your entire operating system.

After going through this situation a few (or several) times, I believe you must be tired.

That's exactly where dotfiles come in to save the day!

What are dotfiles?

Dotfiles are configuration files in plain text, identified by a dot . at the beginning of their names. These files store the settings and preferences of programs that you run daily.

By default, the operating system hides these files. But you can change settings in the file explorer or use the ls -a command to make the dotfiles visible. You can find them in Unix-like systems such as macOS and Linux.

Here are some examples of dotfiles that you've probably already seen:

  • ~/.bash_profile - A script loaded when you log in.
  • ~/.bashrc - A script loaded when you open a terminal.
  • ~/.vimrc - Vim configuration.
  • ~/.gitconfig - Configuration for Git.
  • ~/.config - A folder of many more configuration files.

Why do you need to save your dotfiles?

Sometimes, you need to change computers or format them. When this happens, you will choose the best settings and configurations that match your style and define your workflow. Finally, you will have a development environment that helps you be more productive.

But you'll probably spend lots of time tweaking the setup to make it perfect. So, keeping your dotfiles on hand is a smart way to ensure your workspace is always ready to go.

It is common practice to save your dotfiles in a public repository. This way, you can easily sync them to other systems, simplify your environment, share with others, and perhaps get new ideas from others.

And the best part? You can use version control to your advantage.

You can revert to a previous setup if something goes wrong. You also can try out changes in separate branches without messing up your main setup.

You have a record of all the changes you make over time, so you never lose track of your workspace!

How to create a dotfiles repository?

  1. Set up a folder to store your dotfiles.

    It's good practice to have all your dotfiles in your own folder. A popular option is to create a directory called .dotfiles in your home directory. You can do this by pasting the following command into a terminal.

      mkdir ~/.dotfiles
    
  2. Move existing dotfiles files to the dotfiles folder.

    Move the configuration files you want to an equivalent file path in ~/.dotfiles. You can use the mv command to help you.

      mv ~/.bashrc ~/.dotfiles/
      mv ~/.gitconfig ~/.dotfiles/
      mv ~/.vimrc ~/.dotfiles/
      # and other configs files...
    

    Ensure the directory structure within your home folder is mirrored in ~/.dotfiles. This way, the dotfiles management tools can easily place the files where they belong.

  3. Create symbolic links to the original locations of the dotfiles.

    To make the dotfiles active, you must create symbolic links from the new dotfiles directory to their original locations. Use the ln command with the -s option to establish symbolic links for each relocated dotfile.

      # Create a new link called ~/.bashrc which comes from ~/.dotfiles/.bashrc
      ln -s ~/.dotfiles/.bashrc ~/.bashrc
    

    Making a symbolic link for each dotfile manually is boring and repetitive. If you add more dotfiles to the folder, more symlinks will be needed to create.

    Some people develop a startup script to automate the creation of all symbolic links to solve this problem.

    Using a tool designed for this task is easier, like GNU Stow. You can find other utilities in the Dotfiles Guide.

  4. Personalize your dotfiles.

    Select a dotfile file and use your preferred text editor to open it.

    You'll discover a range of settings that you can modify to suit your preferences.

    However, proceed with caution — these are powerful tools. An incorrect adjustment could cause problems.

  5. Use Git to sync your dotfiles.

    Keeping your files under version control helps you sync your dotfiles across different machines.

    To get started, initialize a Git repository in your dotfiles directory.

      cd ~/.dotfiles
      git init
      git add .
      git commit -m "Initial commit of dotfiles"
    

    Next, push it to a remote repository (e.g., GitHub, GitLab).

      git remote add origin url
      # where 'url' is the url of the repository remote ending in .git
    
      git push origin main
    

    It will allow you to clone the repository on a new machine and set up your environment with a few commands.

Essential tips and best practices for managing dotfiles

Don't push any secrets or sensitive information

Remember, never push any secrets like private keys or passwords to your dotfiles repository, whether public or private.

To help you keep things safe, add a secret scan in your continuous integration, like Gitleaks. This tool will catch any secrets before they are saved in your dotfiles repository.

Be careful when using other people's settings

Dotfiles repositories contain the personal settings of their owner, which means they might not be a perfect fit for everyone.

If certain configurations worked for everyone, those settings would've been set as defaults in the programs.

The best thing is when you know what you are using instead of blindly cloning someone else’s dotfiles.

Use a package manager to install all your productivity apps

A package manager allows you to automate the installation, updating, and removing software apps.

Instead of going to the app's website and downloading it manually, you use a simple command in your terminal, and it takes care of the rest.

Homebrew is a popular choice for macOS users. Windows users can opt for Chocolatey. For Linux users, the managers are a little more varied. The apt-get is a common choice for distributions like Ubuntu.

Customize your operating system

You can customize and sync your operating system preferences across your devices.

MacOS utilizes the defaults command in the terminal for managing preferences. In contrast, Linux systems, such as Ubuntu, use gsettings for configuring graphical preferences.

Mathias Bynens' dotfiles has a lot of macOS defaults listed. Feel free to choose some of the defaults that you found useful.

Create your dotfiles management script

Create a script to install all your productivity applications, populate a directory with all your cloned repositories, apply your OS settings, and more.

Don't forget to print colorful messages with a step-by-step of your script. And, if you like it, print an ASCII art and have fun.

It will help you to set up your workspace in seconds. Also, it is a great opportunity to learn Bash script and Makefile.

Keep your dotfiles organized

As a developer, you'll likely keep and improve your dotfiles throughout your career. Making it the longest project you'll ever work on.

It's a good idea to keep your dotfiles project neat and organized for easy updates and expansions.

Plus, remember to document your workspace. Your future self will thank you.

You can configure GitHub Codespaces with your dotfiles

You can customize your GitHub Codespace environment using a dotfiles repository on GitHub. This personalization applies to each new codespace you create, improving your development workflow.

You just need to have a public dotfiles repository under your GitHub account. GitHub will automatically apply it to personalize your codespace environment.

Conclusion

I've shown you enough to get you started with your dotfiles and given you some good ideas.

There is also a GitHub page dedicated to dotfiles. This page contains cool links to some dotfiles that are seen as references in the community.

I hope that the next time you need to reformat your operating system or switch computers, the process goes more smoothly and makes your life easier!

Thanks for reading! 😉

Yasmin Teles
Written by

Software engineer, content creator, open-source contributor, and lifelong learner.