An Introduction to Programmable Completion in Bash

Command Completion

„Out of the box“, Bash performs completion on the first word of the command line (the command name). Bash will do this for

  • every executable file in any directory listed in environment variable PATH,
  • all built-in commands,
  • all function definitions and
  • all user-defined aliases.

To test this feature, type pulsea in Bash and then press the Tabulator key; Bash expands this string to the word pulseadio-tcp, appends a word separator and places the cursor at the end of the line.

If there are multiple choices, i.e. given the string the user has already typed there are several possible completions that share this string as a common prefix, Bash emits a visual list of these possible completions. For example, if you type pu, this string could potentially be completed to two possible command names: pulseaudio-tcp and the built-in command pushd ( denotes that you have pressed the „horizontal tabulator“ key):

prompt> pu
pulseaudio-tcp pushd

If you type the letter l and then press Tabulator again, only one possible completion remains ( denotes a literal blank space):

prompt> pul
prompt> pulseaudio-tcp

In the following examples it will be assumed that the program name already is present on the command line as the first word. It is also assumed that the first word has been completed, i.e. subsequent to the first word there is at least one word-separating character (i.e. a blank space), and the cursor is located after this separator. The command line buffer now contains this:

prompt> pulseaudio-tcp

The command line interpreter can now determine that it should look up possible completions for parameters of pulseaudio-tcp.