Speeding up your workflow using dotfiles
A simple guide to getting started with dotfiles
I’ve always had a passion for speeding up my own workflows. I’m a logical thinker so any repeating process that I have to do manually multiple times a day just doesn’t make sense to me. If you have to repeat some task multiple times throughout the day, why not automate it?
When I got into programming, things like Git weren’t a thing yet. It wasn’t until I started working with Git and task runners like Grunt (remember Grunt?) that I started using the Terminal. I quickly realized that there were many things that I did repetitively, which had to be optimized of course.
That’s when I found out about dotfiles and everything they could do—my mind was blown.
What are dotfiles?
I’m sure that many people have a different definition for this but I just refer to them as those files that live in your home directory (~
) (on a Unix system).
Every time you open up your Terminal, one of the things that happens is that it looks for a bunch of different files in this directory and when they are present, it executes them.
There are many files that your system looks for and it looks for different files under different circumstances. For the sake of keeping this article simple, I’m going to only be talking about your RC file (~/.bashrc
by default) which gets executed every time you open up a new Terminal window or tab. If you’re interested in learning more about the other files, under what conditions, and in what order they are loaded, make sure to check out this article which goes over all of that.
Speeding up your workflow
Okay, cool, so this RC file gets executed every time I open up a new Terminal window or tab. How do I use this to automate my repetitive tasks?
Before we dive into customizing your RC file, let’s discuss something else first. Right now your terminal runs on a shell called Bash. While Bash is great, there are other shells available that come pre-packaged with tools that will help speed up many of your day to day tasks.
Oh My Zsh
A populair one is Zsh which has things like better tab completion and is more configurable than Bash is. Its advantages are a bit technical, so for the sake of keeping this simple let’s skip the specifics and talk about Oh My Zsh. Oh My Zsh is a framework that implements ZSH, instantly upgrades the look and feel of your terminal and adds commonly used commands to speed up your daily tasks. It takes 10 seconds to install and the benefits are huge. If you would only take one thing away from this article, it should be that Oh My Zsh is definitely worth looking into.
Let’s get specific
Alright so having discussed Oh My Zsh, let’s get specific. Frameworks like Oh My Zsh will help you speed up your workflow by giving you access to tools that solve common problems, but now let’s look at problems that are specific to you.
An easy way to get started with automating your own workflow is to simply wait until you have something that you feel could be done easier, any repeating task really. When I found out about dotfiles, one of the first things I did was create a shorthand for git add --all :/ && git commit && git push
because I found myself doing that often throughout my day.
You can easily do this by opening up your ~/.zshrc
file (or your ~/.bashrc
in case you’re not on the ZSH bandwagon yet) and writing some shell code. Or more specifically, adding an alias:
alias gacp="git add --all :/ && git commit && git push"
Now, when you reload your shell (open up a new Terminal window or tab) and you enter gacp
that command will execute. You can add as many aliases as you want.
At some point you might bump into a repeating task that becomes hard to automate using just an alias. This is when you might want to start looking into writing some functions. Functions are especially great when your solution requires a bit more code or when your code needs to be able to accept arguments.
One of the first functions I wrote was to be able to create a directory and then immediately move into it. This function takes a single argument which is used as the name for the folder: mcd name_of_folder
.
function mcd() {
mkdir "$1" && cd "$1"
}
After a while, having written a large amount of aliases and functions, you’ll find that your RC file will get long and maybe even confusing. This is when the source
command will come in handy. The easiest way to think about this is as a way to import code from one file into another. It will allow you to split up your code into multiple files and then import everything into your RC file. For example: you put all your aliases in a new file you call ~/.aliases
(make sure to give your file a shebang so your system knows what language to execute it in), in your RC file you can then import it as:
source .aliases
What is especially nice about this is that it allows you to move your created dotfiles out of your home folder into a separate folder that could be made a Git repository so you can back them up:
source ~/my_dotfiles/.aliases
The possibilities are endless
This should help you get started with speeding up your workflow. There is so much stuff we haven’t gone over yet though: further customizing your Terminal’s look and feel, VIM customization, custom Git commands, you name it. There is an endless stream of things you can customize and build. Whatever you create, just remember the basics:
- Your RC file gets executed every time you (re)start your shell (open up a new Terminal window or tab).
- It’s all just shell scripting, so you can do anything.
The only thing stopping you is a fear or lack of understanding of shell scripting. But don’t worry! You’re not alone. I didn’t know much shell scripting when I started, and working on my dotfiles has thought me so much.
I searched the web for answers every time I wanted to automate something; like with learning any programming language, it was always a process of trial and error. Until one day, I started to be able to do most things from memory.
When you start to get more comfortable writing shell code, I would highly recommend using Shellcheck which is a linter for shell scripting. Its hints and tips have really helped me figure out how to go from writing shell scripts that just work to (higher) quality code.
In case you’re mostly afraid of shell scripting because of how powerful it is, what might help you is making use of a testing framework like Bats, which lets you write unit tests for your scripts to make sure things work and keep working the way you expect them to.
Learn & Share
Now that we’ve talked about speeding up your own workflow, let’s talk about learning from and sharing with others. There are many people who write dotfiles to speed up their workflow and looking at what they’ve done could help you improve your own.
Github has a dotfiles page which features dotfiles from many different people (including my own) — it’s definitely a good place to start if you would like to see what other people have done.
Once you get to a point where you’re feeling pretty good about your setup, make sure to share it with the world by, for example, throwing it on Github. You might not think that your code is good enough to be shared, and it might feel scary, but sharing our work and knowledge is what makes us all better and smarter people.
Go speed up your workflow, get inspiration from others, learn, and don’t forget to share!
Further Reading
Got excited about dotfiles? Here are some useful links so you can keep learning:
- Getting Started with Dotfiles by Lars Kappert
A more extensive and opinionated overview. - Introduction to Shell Scripting by Flavio Copes
A basic, easy to read, and simple overview of Shell Scripting. - shellscript.sh
A full shell scripting guide that covers all the basics. - Advanced Bash Scripting Guide: Local Variables
Sooner or later you’ll wonder: do all variables have to be global? I’m just giving you the answer in advance. - How to Customize your Terminal with ZSH
Further customizing your terminal’s look and feel. - Introduction To Vim Customization
Customizing VIM, if you’re into that. - Custom Git Commands in 3 steps
The title says it all. - Github Dotfiles
- My Dotfiles