Custom bash tab completion

The simplest way is to add the following to your .bashrc.

_cooltool()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "fooOption barOption" -- $cur) )
}
complete -F _cooltool cooltool

Whenever you type cooltool f[TAB][TAB], the _cooltool() function is invoked. It will work out what your current partial word is and then compgen will work out which options match. These are stored in an array called COMPREPLY which is then displayed. Look at man complete and man compgen for details.

For a nice tutorial check out: Writing your own Bash Completion Function


You might want to take a closer look inside your /etc/bash_completion.d directory on your system.

Also you should read this introduction. The second part of the introduction is the one you need.


Write a bash completion script for cooltool. The bash-completion package comes with scripts for many popular programs, which you can use as examples.

Tags:

Bash