Working more productively with bash 2.x/3.x – Programmable Auto Completion

Programmable Completion

Introduction

A relatively new feature in bash is programmable completion, which has beenavailable since the beta version of 2.04.Programmable completion will be familiar to you if you are azsh user. It also exists, albeit in amuch less usable form, in tcsh.

It’s much easier to demonstrate programmable completion than it is to explainit, so I suggest installing one of the files below and trying it out. If youdon’t like it, you can always disable it for a particular command, disable itcompletely, or remove it from your system. It can be installed and removedvery cleanly, so you shouldn’t be wary of giving it a whirl.

Standard completion

bash has offered many forms of completion since its inception, includingpath, file, user, host and variable completion.

  • Path-name completion

    This type of completion occurs on the first token of the command line, allowing you to complete on executable files. Together with file-name completion, it is the most known and used type.

  • File-name completion

    This allows you to complete on file and directory names at the second and subsequent token position on the command line.

  • User-name completion

    This allows you to complete on user names by prefixing the token with a ~ (tilde).

  • Host-name completion

    This allows you to complete on host names by prefixing the token with a @.

  • Variable-name completion

    This allows you to complete on variable names by prefixing the token with a $.

Programmable completion indefinitely extends the type of completion you can perform.

Download

The following files are available for download. All of them are made availableunder the GNU General Public License.

File Type
bash-completion-20060301.tar.gz tarred and gzipped source for bash 2.05 and later, including bash 3.x
bash-completion-20060301-1.noarch.rpm Binary RPM for bash 2.05-12 and later, including bash 3.x
bash-completion-20060301-1.src.rpm Source RPM for bash 2.05-12 and later, including bash 3.x
Changelog CVS change log of bash_completion
bash-3.2-105.i386.rpm Binary RPM of bash 3.2 patch level 17 (including documentation)
bash-3.2-105.src.rpm Source RPM of bash 3.2 patch level 17
bash-3.1-108.i386.rpm Binary RPM of bash 3.1 patch level 17 (including documentation)
bash-3.1-108.src.rpm Source RPM of bash 3.1 patch level 17
bash-3.0-113.i386.rpm Binary RPM of bash 3.0 patch level 16 (including documentation)
bash-3.0-113.src.rpm Source RPM of bash 3.0 patch level 16
bash-2.05b-63.i386.rpm Binary RPM of bash 2.05b (including documentation)
bash-2.05b-63.src.rpm Source RPM of bash 2.05b
bash-2.05a-service_completion.patch Patch (release 20020207) to bash 2.05a to add service completion
bash-2.05a-55.i386.rpm Binary RPM of bash 2.05a with service completion patch
bash-doc-2.05a-55.i386.rpm Binary RPM of bash 2.05a documentation
bash-2.05a-55.src.rpm Source RPM of bash 2.05a with service completion patch
bash-2.05-group_completion.patch Patch to bash 2.05 to add group completion
bash-2.05-service_completion.patch Patch to bash 2.05 to add service completion (requires prior application of the group completion patch)
bash-2.05-51.i386.rpm Binary RPM of bash 2.05 with above patches applied
bash-doc-2.05-51.i386.rpm Binary RPM of bash 2.05 documentation
bash-2.05-51.src.rpm Source RPM of bash 2.05 with above patches applied

Installation

If you wish to install the binary RPM, execute the following command as root:

# rpm -Uvh bash-completion-xxxxxxxx-x.noarch.rpm

Afterwards, you will find you have a new file on your system,/etc/bash_completion.

Installation of the RPM used to modify /etc/bashrc to source/etc/bash_completion, but this is inherently dangerous, so as of the20030505 release, /etc/bashrc is no longer touched (except to cleanup any additions made by previous versions of the RPM.

Instead, a stub script will be installed as/etc/profile.d/bash_completion.sh, which will call/etc/bash_completion if a suitable version of bash is detected.

If you are installing the source file, either gunzip orbunzip2 it, put it somewhere on your system and source itfrom /etc/profile.d/bash_completion.sh, /etc/bashrc or~/.bashrc. If you are putting it somewhere other than/etc/bash_completion, you will need to edit the top of the file tomake $BASH_COMPLETION point to the correct location.

The line you need to adjust is this one:

BASH_COMPLETION="${BASH_COMPLETION:-/etc/bash_completion}"

If you don’t see the above line, you are probably using version 20060301 orearlier. Please upgrade to a more recent version.

Getting started

The first thing to do is source the completion file. As mentioned above,the best way to do this is by having your shell source it at launch time,but that doesn’t do you any good in the shells that you already have open. Totry out completion in an already open shell, execute the following command:

. /etc/bash_completion

You also need to do this if you have elected to install the software froman RPM package.

Once sourced, you can obtain a complete listing of all commands that haveassociated completions with complete -p. Additionally,declare -f will show you the code of all shell functions, includingthose to which the completions are bound.

To get acquainted with programmable completion, it’s probably best to just typesome of these commands and then hit <Tab> at various positions alongthe command line. Many commands complete on different things, depending on theposition and context of the token on the command line. Thisscreenshot mayhelp you better envisage how completion works in practice.

If you’re a sysadmin type, you’ll find ssh completionparticularly useful, since this completes hostnames based on the contents ofyour known_hosts files. Also, if you NFS mount a lot of hosts, trymount hostname:<Tab>.

Software engineers will perhaps find p4 andcvs completion useful.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s