Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I enable composer to have command line argument auto complete?

I would like to be able to use tab auto completion with composer in my bash shell, the same way I can for example auto complete filenames or git commands.

like image 648
Nils Werner Avatar asked May 22 '13 13:05

Nils Werner


1 Answers

To enable auto complete for composer in bash you need to write a function that returns an array of possible values for the last typed parameter (_composer() in this case) and register it using complete -F function command.

As a complete, working example add the following to your .bashrc (or any other configuration script you might be using)

_composer()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local cmd=${COMP_WORDS[0]}
    if ($cmd > /dev/null 2>&1)
    then
        COMPREPLY=( $(compgen -W "$($cmd list --raw | cut -f 1 -d " " | tr "\n" " ")" -- $cur) )
    fi
}
complete -F _composer composer
complete -F _composer composer.phar
like image 129
Nils Werner Avatar answered Sep 25 '22 14:09

Nils Werner