Use bash aliases for productivity boost.

Use bash aliases for productivity boost.

If you operate on the command line, you will find that there are several commands that you use over and over again. While the makers of many of the most common command utilities have attempted to eliminate extraneous typing by using shortened names (e.g. ls instead of list and cd instead of change-directory), however these are not ubiquitous.

If you are a Javascript developer, imagine how many keystrokes you can save by typing nrw instead of npm run watch each time or by typing pa instead of php artisan command.

Luckily, this is possible because bash allows us to create our own shortcuts and time-savers through the use of aliases and shell functions.

How to create a Bash Alias

Declaring aliases in bash is very straight forward. The command format is,

alias alias_name="command_to_run"

Let's create a common bash alias now. One idiomatic command phrase that many people use frequently is ls -lha or ls -lhA (the second omits the current and parent directory listing). We can create a shortcut that can be called as ll by typing:

alias ll="ls -lhA"

Now, we can type ll and we'll get the current directory's listing, in long format, including hidden directories:

ll
-rw-r--r-- 1 root root 3.0K Mar 20 18:03 .bash_history
-rw-r--r-- 1 root root 3.1K Apr 19  2012 .bashrc
drwx------ 2 root root 4.0K Oct 24 14:45 .cache
drwx------ 2 root root 4.0K Mar 20 18:00 .gnupg
-rw-r--r-- 1 root root    0 Oct 24 17:03 .mysql_history
-rw-r--r-- 1 root root  140 Apr 19  2012 .profile
drwx------ 2 root root 4.0K Oct 24 14:21 .ssh
-rw------- 1 root root 3.5K Mar 20 17:24 .viminfo

The alias that we just created will be available throughout the current shell session, but when you open a new terminal window or log out and log back in, it will not be available.

To make this persistent, we need to add this into ~/.bashrc file. This file is read whenever a shell session begins. So we just need to open this file and add our alias command there.

nano ~/.bashrc

At the bottom or wherever you'd like, add the alias you added on the command line.

alias ll="ls -lhA"

Save and close the file. Any aliases you added will be available next time you start a new shell session.

Now next time when you find yourself typing the same command multiple times, take a moment and create a super handy alias for that, you will thank yourself later. Have fun creating your own shortcuts!