Search code examples
autocompletezshzsh-completion

How do I dynamically select a completion function in zsh?


Say I have a script foo which can be called as:

foo git clone http://example.com

or as:

foo print 12

etc.

How can I make a compdef that delegates to $argv[0] here to allow it to handle completion?


Solution

  • You can redispatch into the normal completion system by calling the _normal function, but first you need to modify some of the state so that it will ignore your program name and possibly its arguments. A very simple version of this can be done with:

    #compdef foo
    shift words
    (( CURRENT-- ))
    _normal
    

    If you need to get more complicated that that (which is likely), you can take a look at the completion definitions for other commands that call _normal, such as the completions for env, sudo, or fakeroot.