Improving Programmable Completion

This recipe was adapted directly from Learning the bash Shell by Cameron Newham (O’Reilly).

Problem

You love bash’s programmable completion but wish it could be more aware of context, especially for commands that you use often.

Solution

Find and install additional programmable completion libraries, or write your own. Some examples are provided in the bash tarball in ./examples/complete. Some distributions (e.g., SUSE) have their own version in /etc/profile.d/complete.bash. However, the largest and most well known of the third-party libraries is certainly Ian Macdonald’s, which you may download as a tarball or RPM from http://www.caliban.org/bash/index.shtml#completion or http://freshmeat.net/projects/bashcompletion/. This library is already included in Debian (and derivatives like Ubuntu and MEPIS), and it is present in Fedora Extras as well as other third-party repositories.

Warning

According to Ian’s README: “Many of the completion functions assume GNU versions of the various text utilities that they call (e.g., grep, sed, and awk). Your mileage may vary.”

At the time of this writing there are 103 modules provided by the bash-completion-20060301.tar.gz library. The following is an excerpted list:

# bash alias completion
# bash export completion
# bash shell function completion
# chown(1) completion
# chgrp(1) completion
# RedHat & Debian GNU/Linux if{up,down} completion
# cvs(1) completion
# rpm(8) completion
# chsh(1) completion
# chkconfig(8) completion
# ssh(1) ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.