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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)