Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.

[ -n "$PS1" ] && source ~/.profile

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.

# Load ~/.extra, ~/.exports, ~/.aliases and ~/.functions
# ~/.extra can be used for settings you don’t want to commit
for file in ~/.{exports,aliases,functions,extra}; do
    [ -r "$file" ] && source "$file"
done
unset file

# Detect and load OS specific settigs
platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
   source ~/.linux
elif [[ "$unamestr" == 'FreeBSD' ]]; then
   source ~/.freebsd
elif [[ "$unamestr" == 'Darwin' ]]; then
   source ~/.osx
fi

.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.

# ------------------------------------------------------------------------------
# | Network |
# ------------------------------------------------------------------------------

# Enhanced WHOIS lookups
alias whois="whois -h whois-servers.net"

# IP addresses
alias ip="curl ipinfo.io/ip"
alias localip='python -c "import socket; print(socket.gethostbyname(socket.gethostname()));"'

# Copy my public key to my clipboard
alias pubkey="more ~/.ssh/id_rsa.pub | pbcopy | echo '=> Public key copied to pasteboard.'"

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.

# Make vim the default editor
export EDITOR="vim"

# Don’t clear the screen after quitting a manual page
export MANPAGER="less -X"

# Larger bash history (allow 32³ entries; default is 500)
export HISTSIZE=32768
export HISTFILESIZE=$HISTSIZE
export HISTCONTROL=ignoredups

.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:

# copy the work directory to clipboard
function pwdc() {
  pwd | tr -d "\r\n" | pbcopy
}

# touch a file while creating directories
supertouch() { 
         if [ $# -lt 1 ]; then
                 echo "Missing argument";
                 return 1;
         fi
         for f in "[email protected]"; do 
                 mkdir -p -- "$(dirname -- "$f")"
                 touch -- "$f" 
         done  
}

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.

set nocompatible
set et
set ai
set sw=4
set ts=8
set ruler

.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.

# Files that might appear on external disks
.Spotlight-V100
.Trashesvenv

# Packages ignore
node_modules
.tmp
.sass-cache
app/bower_components

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.



This post first appeared on The Geeky Way | Technology Inside Out, please read the originial post: here

Share the post

What are dotfiles?

×

Subscribe to The Geeky Way | Technology Inside Out

Get updates delivered right to your inbox!

Thank you for your subscription

×