Custom bash autocompletion

Updated . Posted . Visible to the public.

The bash offers control over the behavior of autocompletion.

The most primitive example is this (just run it in your bash; if you want it available everywhere, put the complete ... line into your .bashrc):

> complete -W "list of all words for an automatic completion" command_to_be_completed
> command_to_be_completed a<TAB>
all an automatic

With complete you define how the specified command shall be completed. For basic needs, -W (as in "word list") should be enough, but you may also specify a function, a glob pattern and many more. complete -p gives you a list of currently defined autocompletions. Behold, thou might not define multiple completions for one command.

I recently built a script that takes a project name and then boots a development environment. The project name is taken from a directory holding all projects, so I created the following completion to save tedious project-spelling:

complete -W "$(ls ~/dev/projects )" devenv

Resources:

Dominik Schöler
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2012-12-04 08:08)