A Little Bit of Love from BASH

A Little Bit of Love from BASH

The BASH terminal on your Mac or Linux PC can be very intimidating if you are coming from a point and click world. A few mistyped commands as the root user can wipe out your entire hard drive or royally goof up your server. It all boils down to the great power / great responsibility thing you hear so much about these days.

Once you start spending some quality time with BASH (default terminal on most popular systems), you may begin to discover all sort of hidden goodies. You eventually realize that you are not the only creative, lazy geek in town. You might be surprised to learn that all those ‘wouldn’t it be cool if. . .’ ideas you have are not pipe dreams, they are only a tweak away. There are tons of command line tweaks available, here are a couple of my favorites.

Case Insensitive Auto-Complete

One super cool default feature is tab-complete. If you are in your home directory /home/charlie and you want to cd to the /home/charlie/mycoolstuff directory, you just have to start typing a few characters and then hit the tab button cd myc + [tab] and if there are no other matches like ‘mycaffeine’ then the matched word will be completed. If you do have multiple matches, then double hitting tab will display all of your matches.

To me, one little annoyance is dealing with capital letters. Desktop and Documents, by default use capital letters, so if I want to open a document or file on the desktop, I have to type ‘D. . .’ instead of ‘d. . .’. This is really not too bad since these two directories are universal and well, you get used to them after a while. But when you start dealing with apps, camel-case code and shared documents, you can become annoyed by the capital letters anywhere and everywhere. You can mess with them if you want, but not me.

I turn to my little buddy .inputrc (in /home/charlie) and simply add this line:

set completion-ignore-case On

Once I restart my terminal, I’m all set. Now when I enter cd doc + [tab] into the terminal, I’m rewarded with the word ‘Documents’ in all its auto completed glory.

BASH History, Auto-Complete

One of the most handy features in BASH is the ability to hit the up arrow and scroll through previous commands. BASH stores these commands in a hidden file in your home directory called .bash_history. You can view this file with the command history or even open it with your favorite editor and see previous commands you’ve run (command line history). The number of commands it stores is controlled by the $HISTSIZE env variable. To see what your system is set to, simply run the command echo $HISTSIZE.

That’s fascinating and all, but aren’t we supposed to be learning about something called BASH history, auto-complete?

Why, yes we are, sorry about that. . .

So, once again, we’re going to turn to (you guessed it, /home/charlie/).inputrc

and add these 2 lines:

"\e[B": history-search-forward
"\e[A": history-search-backward

Now you’re in business. Let’s say, sometimes I rsync my local /var/www directory with a development server. A command may look something like this:

rsync -av /home/charlie/var/www/ devuser@192.168.1.111:/var/www

. . .and, let’s say it’s been a week or 2 since I ran the command and I can’t remember the syntax or IP. I can search through the entire history, or I can simply type rsync into the terminal and hit the up arrow. The results are only previous commands that start with ‘rsync’. I can easily scroll through only the rsync commands in my history.

There are so many interesting shortcuts you can use, these just happen to be a couple of my favorites. Tune in next time when I show you how to replace devuser@192.168.1.111 in the above command with the single variable dev.