What are dotfiles?

What happens when your hard disk fries, or eventually you want to get a new machine? You tend up to loose all your dev configuration after years of hard work tweaking things here and there according to your liking.

Configuring a dev machine according to your liking would’ve been a really daunting task until and unless there were dotfiles.

Introduction

These are simply the files whose names start with a dot (.) like .vimrc, .bashrc, .ssh, etc. And any file that begins with a dot is treated as hidden in all *nix operating systems. Therefore, having lots of dotfiles even in your home directory does not make it looks clumsy.

Where are dotfiles needed?

They are mainly used for software configuration. Therefore, they are also known as configuration files. We can always configure different tools that we use daily, easily using dotfiles.
They can also be used to automate a series of package installations. Their usefulness lies in the fact that anytime you can buy a new system and configure it like, you’ve been using it for years.

How to use dotfiles?

In order to maximize their utilization, we should save our dotfiles somewhere. That may be a USB drive, cloud storage and what most people use nowadays is, the GitHub repository. The idea is, they remain within our reach while switching between different machines.

There are different dotfiles for different purposes. For example, .bashrc to configure bash and similarly .zshrc to configure zsh. We can also create our own dotfiles for different purposes like .aliases to declare all modified commands and .export to set environment variables we need. In this tutorial, we’ll see a couple of examples to show the working of these dotfiles. Let’s go ahead!

.bashrc

This is the default file, that runs every time when we open our bash. Either we can directly write all the bash configurations in this file or we can use a distributive approach, like having functions in one file and OS specific settings in different files, etc. Following is an example of a .bashrc.

This means that, if “$PS1” will return a non-empty string, then load .profile.

.profile

We may use this to load OS-specific settings, and all other dotfiles, like .functions, .aliases, .exports, etc. Let’s take a look.

.aliases

An alias can either be a modified command, or a shorthand for it that we may define ourselves. Suppose, for git commit, we always write git commit -m "commit message". In this case, we can create an alias by alias gcm=git commit -m with this, gcm is same as git commit -m. Following is a gist of a .aliases file.

The complete file is here.

.exports

Export is widely used to set environment variables. Also, we export a variable, when we need it in other programs. That depends upon the shell as well. For bash, export makes a variable usable, in the environment for child processes. For more about export, you may see GNU bash manual. Your .export file may look like this.

.functions

If we want to create an alias that not just perform a shorthand or a group of commands but, also which behaves conditionally and can work with variables, then we have a better approach of “functions”. A function is an independent block of script, which is more powerful than an alias because they can accept variables. We can create functions for various purposes like:

More functions are available here.

.vimrc

This dotfile is specifically used for “vim” configuration. All the necessary commands that we use very often, we can put them in this file. We can also set up different variables of vim, with the value we desire while working on it. For Unix and MacOS, this file is always used and is recommended at ~/.vimrc. To know more about .vimrc you may open the “vim” and use the command :help vimrc-intro.

.gitignore

This file is used in the case, we want to avoid or ignore some files or folders while git makes a commit. These files may include Thumbnail cache files, local virtual environment, etc. If we want a particular file say Example.tmp to be ignored. Just give its name in this dotfile. And if we want to ignore all .tmp files then put *.tmp in .gitignore. A gist of this file is shown below and a complete file is available here.

Conclusion

We’re keeping this post short, as the main motive was to explain how you can store the configuration of your machine. The whole purpose of dotfiles is to make our work as easy as possible. These hidden files give us a comfortable & quality working experience.
Dotfiles are specific to an individual because of different people like different configuration for the same tool. Go ahead and explore some of the best dotfiles out there. You’re free to take inspirations and create your own 🙂

You can have a look at one of the dotfiles repo here: https://github.com/CuriousLearner/dotfiles

If you’ve any queries, please ask in comment section below.

An open source enthusiast, who feels passionate about web development!

Leave a Reply

Your email address will not be published. Required fields are marked *