The stuff I do

Creating zsh completion function

← Notes

Tags: [shell][zsh]

A quick trick to have custom completion on the aliases I create.

First create the function and the alias, they can be in ~/.bash_aliases or whatever.

myCoolFunction() {
echo "Here are the args from my function $@"
}
alias mcf='myCoolFunction'

Then create the completion function (best practice underscore+function name) which can be anywhere too. An interesting list of the helpers provided by zsh for the completion functions is here.

_myCoolFunction() {
compadd "$@" foo bar baz
}

Finally in ~/.zshrc we can register _myCoolFunction as the completion function of myCoolFunction:

compdef _myCoolFunction myCoolFunction

Once everything is ready in a new shell typing mcf <TAB> should yield the 3 completions options.

If things don't work one can check the completion function for a particular function (It does not work with the alias) with:

echo $_comps[myCoolFunction]

and the implementation with

functions $_comps[myCoolFunction]

Finally I should check how to make it work with the defined files (~/.oh-my-zsh/completions) and understand why the compdef command only works when put in .zshrc.

← Notes