Use aliases for faster BASH shell navigation

The BASH shell is very powerful and allows an experienced user to get things done quickly. One area that new users find frustrating is directory navigation. Coming from a GUI environment, it would appear that one is able to navigate around a file system much more quickly in their GUI file browser. As an example, say that I need to access my Dropbox folder very often. The workflow in a GUI environment would be:

  1. Open your file browser (Dolphin, Thunar, etc.)
  2. Click on the shortcut in the left hand column of the browser.

That is very simple! However, there are some inefficiencies there that can be removed. These inefficiencies are:

  1. The user needs to move their fingers off of the home row navigation of the keyboard.
  2. The user must switch away from the terminal environment and open another program outside of the terminal.

This may not seem like a big deal. After all, you can leave your GUI file browser open and simply Alt + Tab to it. For the vast majority of users this workflow is just fine. For those that want to remain in a terminal environment, reduce distraction caused by multi tasking many applications on their desktop, and be more efficient, I recommend the following. We’ll use the same example above of wanting to navigate to my Dropbox folder that I access very often. This requires the following initial setup:

  1. Edit your .bashrc using vim ~/.bashrc or whatever your preferred text editor is.
  2. Create the following alias: alias drp=’cd $HOME/Dropbox/’
  3. Save the file.
  4. Reload your ~/.bashrc by typing source ~/.bashrc

Now that the initial setup is done, this is how you can navigate to your Dropbox folder from the command line:

  1. Type drp and press Enter.

If you perform each of these workflows one after the other several times, you’ll notice that using the BASH alias is consistently quicker. However, you can’t see the contents of the directory immediately as you can in the GUI file manager. We can take our alias even further to resolve this by changing it to run ls once the first command is completed. I personally do not do this, but I’ll show it as an example:

alias drp=’cd $HOME/Dropbox/;ls’

These are my current directory navigation aliases. I’m sure I’ll be adding more to them in the future. The goal when creating these aliases is to keep them as short as possible so that they require a minimum of input, but that they be memorable and descriptive.

alias hm='cd ~'
alias mu='cd $HOME/Music'
alias doc='cd $HOME/Documents'
alias app='cd $HOME/Applications'
alias dsk='cd $HOME/Desktop'
alias pic='cd $HOME/Pictures'
alias dwn='cd $HOME/Downloads'
alias drp='cd $HOME/Dropbox'
alias abl='cd $HOME/Dropbox/Public/Ableton'

In addition to the above aliases, there is a BASH built in command I use all the time. Typing cd – will toggle you between your current directory and the last directory you were in.

Directory Navigation Utilities

There are some directory navigation programs that you can download and run in your BASH shell that will learn frequently visited directories. Once learned, they allow you to jump to them quickly. Some examples are autojump and z. These may work for you, and you should try them! My reasoning for not using them is I want to keep my configuration as minimal and portable as possible. Only having to download my ~/.bashrc and not install any additional programs keeps things simple. Additionally, if I did use that utility and had to install it on another system, I would have to teach it my common directory habits again. This means there would be a period of time where it would have to again learn my habits.


Posted

in

by

Tags: