How do I set Zsh autocompletion rules for second argument (of function) to an existing command's rules?

I found /usr/share/zsh/functions/Completion/Unix/_git which had some tips for aliases like this and ended up defining these functions for the aliases:

_git-ls () {
  # Just return the _git-ls-files autocomplete function
  _git-ls-files
}

Then, I did a straight compdef g=git. The autocomplete system will see that you are running, for example, g ls and use the _git-ls autocomplete function.

Thanks to user67060 for steering me in the right direction.


I had to do something very similar so this is roughly what should solve your problem.

_g () {
    case "${words[2]}" in
      ls) words[1,2]=(git ls-files);;
      g) words[1,2]=(git grep);;
      *) return 1;;
    esac

    _git # Delegate to completion
}
compdef _g g

One thing to note is that if you change the number of arguments you will need to adjust the $CURRENT variable.