Dan's notes, etc

Add commandline completion to your command

For this explanation assume your new command is named sc, and sc is on your path. We’ll also assume you are replicating the completion of an existing command named systemctl.

Reference:

First find out where to put your custom completion script.

This presumes you’ve installed bash-completion.

pkg-config bash-completion --variable compatdir
# On my machine (2023-05-18) that is
/etc/bash_completion.d

Second find out if systemctl has a custom completion script.

$ complete -p systemctl

There are two categories of output from complete -p <cmd> The first tells us the obvious, that there is nothing special about systemctl, and it is completed with bash’s default completion.

bash: complete: systemctl: no completion specification

If this happens check to see if bash_completion is being sourced.

The second category of output looks like this:

$ complete -p systemctl
complete -F _systemctl systemctl

This tells us that bash uses a function named _systemctl when completing the command systemctl.

Third find the _systemctl function.

I could tell you a lot about find and grep, but that would just waste your time with learning. Instead, just look in /etc/share/bash_completion/completions. There I see a file systemctl, and inside it the function _systemctl is defined.

Fourth create your custom completion file in the directory for custom completions, /etc/bash_completion.d/.

cat > /etc/bash_completion.d/sc << EndOfFile
#! /usr/bin/env dash
# completion for sc


source /usr/share/bash-completion/completions/systemctl

complete -F _systemctl sc
EndOfFile

Fifth test it or don’t. Either way this tutorial is done.

You may have to source your new completion file before testing.