When you use Ubuntu on a daily basis, you probably know there is something there called “The Terminal”. Ubuntu Desktop users should know the gnome-terminal in particular. If you are Ubuntu user, but have no idea what I’m talking about, check Applications -> Accessories -> Terminal. Keep in mind that within this terminal, the ease of destructing stuff is enormous!
When working inside your terminal, a feature that quickly becomes inevitable usable, is the bash completion. All it does is completing your commands in an easy way, speeding up your productivity enormous. Beside this speedup, perhaps it’s most valuable function is that it can help typing. And by saying it can help, I mean that the terminal makes your sentences complete! So it minimizes type errors, and long command line arguments become more “fun” using!
An example of how it works. Say you want to go to the terminal and start your favorite browser from there. For the use of this example it really is unimportant why you want to do this. On a regular chosen system, the preferred browser could be Firefox. Any other type or brand of browser can be used in this example. You managed to start your terminal. By now, you forgot what the command was to start Firefox. You only remember if starts with “fire”. So start typing fire and as you get to the letter e, press the ALT-key on your keyboard.
Two different things can happen next. One is that bash completes your command, so it should look like ‘firefox’. Or, bash will show a list with commands that start with ‘fire’. In the first case you just can hit the enter key. Second case is to keep on typing more letters en then hit the tab key again, or type the command in whole yourself (as the terminal has reminded you of the command you want).
Second very neat feature I want to talk about here, is that the completion also completes your arguments! A mostly common used program on terminal could be Apt for some people. From user perspective, apt-cache can be used to search for packages you wish to install. So, what arguments do I have to pass to apt-cache to search for my needed package? Fire up your terminal. Start typing “apt-cache” press the TAB key, note that bash gives you a space. Press TAB again, and viola, you get a bunch of arguments!
Assuming you got found out that searching packages should be done with “apt-cache search <package>”, I will give you another nice completion feature. This feature actually comes in handy when you perform administrative task on terminal. Some people, like myself, install software packages through apt. You can do this by typing “sudo apt-get install <packages>”. When apt finds the package he assumes you want to have, he installs it for you.
What if you know the package you would like to install starts with “blue”? Simply type “apt-get install blue” and hit the TAB key. Next, bash will give you some output of packages you can install, starting with “blue”. Neat isn’t it?
Something I noted afterwards, is that when you perform multiple tasks as root user, it is easier to become root for certain amount of time. Doing this, will disable your auto completion! So, how do you solve that!? Actually it isn’t hard to fix this. Just open up /etc/bash.bashrc in your favorite text editor (as root user off course) and search for the lines that look like this:
# enable bash completion in interactive shells #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then # . /etc/bash_completion #fi
Simply remove the hash tags so that it looks like this:
# enable bash completion in interactive shells if [ -f /etc/bash_completion ] && ! shopt -oq posix; then . /etc/bash_completion fi
Save the file and you’re done!
From now on, becoming root, also gives you the benefits from bash auto completion!
Everything above was tested on Ubuntu 10.04. See http://embraceubuntu.com/2006/01/28/turn-on-bash-smart-completion/ for some more information for bash completion for the root user.