Search code examples
zsh

optimal way to debug zsh competion functions


When we run compinit, it redefines all the widgets which perform completion.

If I am working on a completion function, I can just use rm -f ~/.zcompdump; compinit to see how my new completion function is working.

However, I think there is a more efficient way to do it.

Suppose I wrote the following completion function from zsh-completions

#compdef cmd
local -a subcmds
subcmds=('c:description for c command' 'd:description for d command')
_describe 'command' subcmds

How can I quickly modify small part of the function and see how the function is working.


Solution

  • There's never any need to rerun compinit. You can use compdef to define your new completion function provided it can be found in $fpath, for example:

        compdef -a _cmd cmd
    

    The -a marks _cmd for autoloading.

    I tend to work in a temporary directory or local git checkout so define the following alias to quickly add the current directory to the start of $fpath:

        alias -- +fpath='fpath=( ~+ $fpath)'
    

    It can also be useful to reload autoloadable functions when you've modified them. For this I have the following function which also removes functions that were defined in the function:

    freload() {
      local func
      for func; do
        unfunction ${(k)functions_source[(R)${functions_source[$func]:-X(#s)}]:-$func};
        autoload -U $func;
      done
    }
    

    Another thing I do is I have compdef _foo foo already defined in my .zshrc which makes it very quick and easy to test things out just by defining an _foo function directly on the command-line.