Ubuntu, a Linux distribution that is getting popular because it has successfully improves it’s user friendliness with simple User interface. You can install any applications with few clicks, configuration under GUI dialogs etc. People usually have the thinking of maybe one day, Ubuntu will become another new operating system like Microsoft Windows.
Never! The concept of making everything easy by using graphics user interface is wrong. Ubuntu Feisty have bundle with tons of new GUI applications to ease the job of configuration such as networking, wireless access, vpn, multimedia codec installation etc, but it also packaged with some amazing command line interface features, one of them is bash completion.
Most of us may had experienced the conventional bash completion by hitting tab twice. For example, while typing a command name halfway and tabbing two time, it will shows you a list of available commands. Let say i type
will list all these
svn svnadmin svndiff svndumpfilter svnlook svnpath svnserve svnsync svnversion
So what is so special about the programmable bash completion?
What if bash completion is also available on certain commands? such as apt-get, svn, help etc?
We usually need to refers back what options a command have by specified –help or -h. What if the command have auto completion on its options?
Lets look at svn,
svn c[tab][tab] cat checkout ci cleanup co commit copy cp
This shows you that you can do svn cat,svn checkout, svn ci etc.
Not only auto completion on options, seems that it is programmable, you can actually write a function and bind it with a command. Lets look at apt-get for example. Conventionally, if we wants to list all available packages with some leading characters, for example ‘can…’, we need to do this
apt-cache pkgnames can
But now you can do this,
apt-get install can[tab][tab]
Pretty cool isn’t it?
Another example, bash completion can bind with certain file format with certain file extension. Let say you have a folder name download, where you have stored all archive files such as tar.gz, .zip, .rar etc. When you do this,
tar zxvf [tab][tab]
It will list all the files with extension of .tar.gz only.
How to install?
Certain Linux distribution may have already installed, you just need to turn it on by uncomment these lines in /etc/bash.bashrc
# enable bash completion in interactive shells if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi
You may try to apt-get if you are using Debian based distro.
apt-get install bash-completion
Looking for source code, and manual installation? http://www.caliban.org/bash/
I was wandering what commands has the ability of bash completion?
complete | sort -o complete.txt
Refers to the command line above, I have store all the information of what commands have bash completion into a file complete.txt. To define bash completion, we uses a bash command call ‘complete’. The complete.txt list all the complete command line. It might look a bit complicated,
... complete -F _wvdial wvdial complete -g groupdel complete -g groupmod complete -g newgrp complete -j -P '%' disown complete -j -P '%' fg complete -j -P '%' jobs complete -o default -F _aptitude aptitude complete -o default -F _aspell aspell complete -o default -F _configure_func configure complete -o default -F _cvs cvs complete -o default -F _dpkg_reconfigure dpkg-reconfigure complete -o default -F _gpg gpg complete -o default -F _iconv iconv complete -o default -F _lftp lftp complete -o default -F _longopt env complete -o default -F _longopt netstat complete -o default -F _longopt seq complete -o default -F _longopt uname complete -o default -F _longopt units complete -o default -F _longopt wget complete -o default -F _look look ...
You may not understand what -F -o is for at first, but it does provide hints of what commands have bash completion by looking at the last few words in each line. For examples, wvdail, groupdel … units, wget, look have bash completion.
Where do I learn how to program on bash completion for my packages?
Bash man page, or online bash references. If you just want to understand the meaning and usage of the complete’s options, read Programmable Completion Builtins.
The code is actually at /etc/bash_completion, with all the function definition.
Other bash completion articles?
[tags]bash, auto completion, bash completion, bash programming[/tags]