Read more

Custom bash autocompletion

Dominik Schöler
December 04, 2012Software engineer at makandra GmbH

The bash offers control over the behavior of autocompletion.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

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:

Posted by Dominik Schöler to makandra dev (2012-12-04 09:08)