About     Search     Feed

Nilesh D Kapadia


Music     Twitter     Github

Bash Completion

The Bash shell does tab completion executables, bash commands, and files/folders out of box. But it’s completion can be extended to support even more types of completion including arguments for commands and things like hostnames through the use of programmable completion.

There is a package called bash-completion which includes a set of completions for various utilities. For example, for Subversion, if you type svn <tab><tab> (replacing <tab> with actually pressing tab) it will give you a list of all the svn subcommands that are available. If you type svn rev<tab> it will complete it to svn revert. For ssh, if you type ssh <tab><tab> it will list all the hostnames and IP addresses it knows about (it looks like it uses a combination of what’s in /etc/hosts and ~/.ssh/known_hosts). You can of course do ssh username@<tab><tab> if you are using a specific username.

Once the bash-completion package is installed, you can copy additional completion scripts to /etc/bash_completion.d and they will be automatically included. I did this with the Maven 2.x completion script.

Git has a completion script that is included in its standard distribution. Find out where you have Git installed, and source the script GIT_INSTALL_DIR/contrib/completion/git-completion.bash in your .bash_profile. For example, I have Git installed via the binary installer for OS X Leopard, and thus I added this line to my .bash_profile:

source /usr/local/git/contrib/completion/git-completion.bash

Note that this is independent of the bash-completion package described above and does not require it (since we are sourcing it directly).

Django also has bash completion script included in its distribution. Replacing $DJANGO_DIR where you have the release extracted (or checked out from version control):

source $DJANGO_DIR/extra/django_bash_completion

With this, you get completion for django-admin.py and manage.py. For manage.py you need to set execute permissions and run it as ./manage.py on the command line rather than python manage.py in order to get completion for it.

© 2013 Nilesh D Kapadia