Posted over 8 years ago. Visible to the public.

Custom bash autocompletion

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):

Copy
> 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:

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

Resources:

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
over 8 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more