How to call git commands without ‘git ‘ in front

commandgitzsh

How do I tell my zsh to automatically try a command with git in front, if the command is not found? E.g. I want to run $ status and if there is no status in $PATH, my zsh should try git status.

Best Answer

This sounds fragile — you could get into the habit into typing foo instead of git foo, and then one day a new foo command appears and foo no longer invokes git foo — but it can be done. When a command is not found with normal lookup (alias, function, builtin, executable on PATH), zsh invokes the command_not_found_handler function (if it's defined). This function receives the command and the command's arguments as its arguments.

command_not_found_handler () {
  git "$@"
}

If you want to do some fancier filtering, the command is in $1 and its arguments can be referred to as "$@[2,$#]".

command_not_found_handler () {
  if …; then
    git "$1" "$@[2,$#]"
  fi
}
Related Question