It is possible to provide tab completion capability for any executable that can be invoked via shell (In fact, any shell input can be auto completed. It doesn't need to refer to an actual executable.).
However implementing an auto-completion script can be quite tedious when the required argument scheme gets complex. Also there is no trivial way for implementing an autocompletion script without utilizing different tools with complicated syntaxes.
This is a simplified template that can be used for providing auto-completion support for any command-line tool.
Download/copy completer.sh to a persistent location (rename if needed).
Example:
mkdir -p ~/.config/autocompletions wget -P ~/.config/autocompletions https://raw.githubusercontent.com/SumuduLansakara/custom-bash-completion-script/master/completer.sh
Define the name of target executable as by modifying the last argument of complete command in completer.sh script.
Example:
If the name of executable is
mytool,completer.shshould have the following.complete -F _get_completions mytool
Define desired argument scheme by modifying the "user defined argument scheme" section in completer.sh script.
Example:
If the executable takes two arguments
show info, i.e.$ mytool show infoauto-completion support can be added as follows,
_opts=(show) # first argument is `show` show_opts=(info) # second argument is `info` when first argument is `show`
Source completer.sh script in the current shell to enable auto-completion support immediately.
Example:
source ~/.config/autocompletions/completer.sh
Add above line to .bashrc to make this persistent.
Example:
echo 'source "${HOME}/.config/autocompletions/completer.sh"' >> ~/.bashrc
Now the target executable should have auto-completion support.
Enter executable name and press <Tab> key to see shell completing the remaining word.
Examples:
$ mytool <Tab> # result: mytool show $ mytool sh<Tab> # result: mytool show $ mytool show <Tab> # result: mytool show info $ mytool show i<Tab> # result: mytool show info
Hints:
- Refer
exampledirectory for a slightly more complicated usage example.