This is the first of two parts on Bash completion. Part two ishere.
Over the years I ‘ve developed a command-line tool I use for routine tasks such as provisioning my machine, generating project templates and managing secrets. The tool is written in Ruby and I invoke it with theZZ
command.
Most ofwhatit does is Fairly straightforward. The clever bits are usually delegated to something else. For example,zz provision
is really just a wrapper that installs and runs Chef, while passing various options to it.
Recently, I added Bash completion to my tool. I’ve wanted this for a while, but decided to add it now in preparation forsecrets management.For example, I want to be able to typezz secret --readamaz
and have it complete tozz secret --read amazon /
. Perhaps hitting
again will list all secrets under this path, eg username, password, access_key, etc.
The mechanics
InBash, completion is handled through thecomplete
‘built-in’:
$typecompletecomplete is a shellbuiltin
This command allows you to register a method of completion for a command. For example, anrgb
command might register its known colors:
You could then complete color names:
($ color(TAB)>TAB>blue green orange pink p urple red yellow$ color pTAB>TAB>pink purple)$ color piTAB>
The- W
switch configures a static list of completions that are printed in alphabetical order. It’s just one of the many methods ofcompletion.
Listing completion methods
To see which commands have completion methods, runcompletewithout arguments:
Here you can seeNodenv
andrbenv
support completion. They use the- F
switch to specify functions to handle their completion,namely_ nodenv
and_ rbenv
. When you complete one of these commands, their output is context-aware:
$ rbenvinstall2.(5) *********TAB>TAB>**********2.5.02.5.0-rc12.5.1**2.5.22.5.3
That’s helpful!rbenv
has kindly listed which Ruby (2.5.x) versions are available to install. We could find this out fromrbenv install --list
but that’s inefficient because we’d have to clear our current command then re-type it.
How completionfunctionswork
When a function is registered as the method of completion with the- F
switch, it must comply with an ‘interface’ of sorts. When the function is called, Bash sets some environment variables to be used by the completion function.
Theytellit the contents of the command-line, the cursor position, etc. For example,$ COMP_LINE
contains the full line that was typed,$ COMP_WORDS
is that same line broken into an array of words and$ COMP_POINT
is the cursor’s index position.
In return, the completion function should set$ COMPREPLY
to specify which completions to print for the command.
An example
Everybody loves FizzBuzz, right? Let’s demonstrate Bash completion with a custom function thatmagicallycompletes the next term in the sequence:
Ourcommandis calledfizzbuzz
so we name our completion function_ fizzbuzz
, as per the convention . We first set thelength
variable to the number of words on the command-line and(number)
to one less, since ‘fizzbuzz
‘itself counts as a word.
We’ve probably all seen FizzBuzz before so let’s skip the modulo logic. The important part is to set$ COMPREPLY
– in this case, to an array of the next term in the sequence.
Now, if we typefizzbuzzfizzbuzz
command doesn’t actually exist but that doesn’t seem to matter!
As you can see, there’s plenty of fun to be had! Inpart twowe’ll implement Bash completion for my automation tool and see how it works in practice.
GIPHY App Key not set. Please check settings