Stop typing so much in the command line!

Automate some typing in the command line

As a developer, I used the command line a lot. In fact, I might actually prefer the command line over a graphical user interface (GUI).

The command line has many advantages that a GUI doesn't. Automation from the command line is very easy to accomplish by placing your commands sequentially in a script. The GUI requires use to use some type of software to interface and automate the GUI.

For me, the command line can be cumbersome when I am writing some commands that are very long or that have a lot of flags. Here is an example of one that I use when I am testing something I wrote in Django:

python runserver --settings=project.settings.development

This command is telling Django to start up using the development settings, running on my machine, using port 8001. On my machine, this command will rarely change. Yet, that is a really long command to type out just to start up my development server. The same can be said of other web development frameworks. Each framework has commands that are very long and for the most part do not change often.

To this end, and due to the fact that I have never been paid for each character that I type, I use aliases on my Linux machine to help with these long and/or verbose commands.

Full disclosure, I do not use an alias command outside of my development machine. When I have automation running on a machine that isn't my development machine, I like to be explicit with each command I am running. That way, it is easier to find out what is going on and what might have gone wrong in that environment.

Find the commands you use the most

I use this command to get my most commonly used commands on my machine:

history | cut -d' ' -f 5- | awk {$1=$1};1' | sort | uniq -c | sort -n

This will grab all the commands in my history and skip the index number at the start of the history. The awk part of the command will trim the white space on each line. The last three commands will make sure the commands are unique with the most used commands showing up at the bottom of the output.

Using this information, now I can get to work and find common commands that I use a lot and do not want to type out over and over. Once I have a list of commands that I commonly use, I then turn them into aliases so that I use those commands without having to type very much at all.

Setting up aliases to save time typing

Returning to the runserver command above, I have the following in my ~/.bashrc file:

alias drs="python runserver --settings=project.settings.development"

Now that the alias has been added to the ~/.bashrc file, you need to create a new terminal session and then you can just run drs to start your Django development server.

Aliases can also be used to shorten commands so you can get to the meat of the command quickly. For example, in Rails, when I want to generate a controller, I don't want to type out the prefix of bin/rails generate controller. I just want to type out the part of the command where the controller gets generated. So I have the following alias for this task:

alias rgc="bin/rails generate controller"

Now when I want to generate a controller in rails it is much shorter. Here is a comparison with and without the alias:

bin/rails generate controller Say hello goodbye

rgc Say hello goodbye

That is much shorter and allows me to get to the part of the command I am really interested in.

I want to point out a few things about the alias names that you pick. First, make sure you do not use an alias for a package in your package manager. In Ubuntu, you can type in the alias candidate into the command line and if there is a package that uses that command you will get a message like the following:

The program 'alias-candidate' is currently not installed. You can install it by typing:...

Another thing you should think about as you create aliases is about a consistent naming convention. All of my Django aliases start with the letter 'd'. All of my Rails aliases start with the letter 'r'. Since both frameworks have a lot of commands that do the same thing, I can change the prefix of the alias to accomplish the same thing in both frameworks. For example, if I am going to start the development server both frameworks have a very similar alias:

drs for Django

rrs for Rails

The 'rs' part stands for run server. The prefix lets me know if it is for Django or Rails.

Aliases has saved me so much time typing and trying to remember all the flags for a command. If I need to know the exact command it is documented in my ~/.bashrc file. I hope it saves you time as well.

Happy Coding!