5

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.

  • 7
    Have you considered making shell aliases for this? It's not really what you describe, but it would get the job done IMO. – Brandin Sep 20 '15 at 10:45
5

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
}
  • Or "$@[2,-1]" instead of "$@[2,$#]". Note that "$1" "$@[2,$#]" is "$@". – Stéphane Chazelas Sep 21 '15 at 10:15
  • 2
    Very fragile: git rm vs rm. – chepner Sep 21 '15 at 14:32
2

I suggest that do not creat problems for yourself, just checkout oh-my-zsh, and put

plugins=(git)

in your .zshrc, then you can see git status by typing gst.

...and a lot of others, checkout git.plugin.zsh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.