Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

License made-with-bash Stars Watchers

Bash Tab-Completion Script Template

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.

Usage:

Step 1:

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

Step 2:

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.sh should have the following.

complete -F _get_completions mytool

Step 3:

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 info

auto-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`

Step 4:

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

Step 5:

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 example directory for a slightly more complicated usage example.

About

Boilerplate for generating custom bash auto completion scripts

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages