1988

How to Get ZSH to Complete CMD1 Like CMD2

I have a wrapper function, g, in my zsh configuration:

# By itself: run `git status`
# With arguments: acts like `git`
function g {
  if [[ $# > 0 ]]; then
    git $@
  else
    git status --short
  fi
}

Now I can type g add instead of git add; with no arguments, I see the git status. But by using this wrapper, I lost tab completion: g checkout [TAB] didn’t autocomplete branches.

The fix is to add this code to your zsh config:

# First, make sure you're initializing ZSH's completion:
autoload -U compinit && compinit

# This is the magic line!
compdef g=git

compdef g=git tells ZSH to complete g as if I’d typed git.

For more, try reading the docs, which are (charitably) dense, or (less charitably) almost intentionally opaque.