Search code examples
bashtab-completion

Call bash-completion for another command from within completion function


_example()
{
    local cur prev

    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    if [[ ${prev} == example ]] ; then
        COMPREPLY=( $(compgen -W "git" -- ${cur}) )
    elif [[ ${prev} == git ]] ; then
        _command
    elif [[ ${prev} == g ]] ; then
        # ??? TODO 
        # I want the same completion for `example git <TAB>` and `example g <TAB>`
    else
        COMPREPLY=()
    fi
    return 0
}
complete -F _example example

I know how to "call" bash-completion for next command. (code above)
eg.: example git <TAB> => completion for git

How to make bash-completion that "calls" any completion I want for next command?
eg.: example g <TAB> => completion for git


Solution

  • Populate COMP_CWORD, COMP_WORDS, and COMP_LINE, and then call git's completion function __git_wrap__git_main.

    For an example of populating COMP_* and invoking a pre-defined completion function, see this.

    For more on figuring out how git completes, you might want to start here.