Sunday, March 20, 2011

Linux Usage: Turn on Bash Smart Completion

Bash shell provides an awesome feature that increase the accessibility in command line. Some files' name are long and you probably won't want to type it or maybe you type it wrong easily. Auto-completion is a good function for you which you just need to type a few characters only. For example, you can type

$ cd Dow [press tab key]

it will help you to complete the command to

$ cd Downloads

Sometime, bash shell can't identify your file name since there are multiple files with same initials. For example, you type
cd Do [press tab key]
You may see
Documents        Downloads
Then now you need to input more key to specify the file name

In fact, bash shell in ubuntu is smarter now, you can find that codes which enable smart completion in the bashrc file.
if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi

If you view /etc/bash_completion, it defines which program to use which file type. For example,
complete -f -X '!*.@(pdf|PDF)' acroread gpdf xpdf
which means for program "acroread", "gpdf" or "xpdf", when you press tab key under bash shell, it will find files with pdf or PDF extension only.

However, you may find that for sqlite3, the default support extension is .sqlite. Sometimes, I use .db as sqlite3 file. Thus I changed it to
complete -f -X '!*.@(sqlite|db)' sqlite3

No comments:

Post a Comment