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 Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)