Smarter tab-completion in bash

Here’s something cool.

I was talking to my physics professor, who was running SuSE 10.1 Beta on his laptop, and he showed me an interesting feature of SuSE. In the terminal, when using tab autocompletion, bash was smart enough to tell which type of files you are looking for. For example: if, in my home directory, I have a folder named ‘folder’ and a text file named ‘file.txt’ then in a normal bash prompt, typing ‘cd f’ and then pressing tab will display both ‘folder’ and ‘file.txt’.

However, in SuSE, pressing tab after the ‘f’ will autocomplete to ‘folder’ because SuSE was smart enough to know that the ‘cd’ command takes a folder as an argument, so ‘folder’ was the only logical choice. Obviously, I had to have this feature in Ubuntu, and fortunately, it’s easy to do in any Linux distribution. Simply open /etc/bash.bashrc (this requires root access) in your favorite editor, and uncomment the last three lines so they look this this:

if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi

then restart and bash will use its smarter autocompletion.

Thanks to Professor Komp for showing me this nice feature.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>