Skip to content

Instantly share code, notes, and snippets.

@qstrahl
Last active October 4, 2016 19:38
Show Gist options
  • Save qstrahl/ecf9da183beae07b35b23dc2a461fbd3 to your computer and use it in GitHub Desktop.
Save qstrahl/ecf9da183beae07b35b23dc2a461fbd3 to your computer and use it in GitHub Desktop.
# git commands which will be masked for brevity inside git repos
git_cmds=(amend branch checkout commit diff grep log ls-files reflog remote reset stat status)
# create functions for each git command that fall back to the nongit command outside of repos
function $git_cmds () {
if git rev-parse --git-dir &>/dev/null; then
git $0 "$@"
else
command $0 "$@"
fi
}
# define completion functions for all these custom commands
function _my_${^git_cmds} () {
local cmd="${0#_my_}"
if git rev-parse --git-dir &>/dev/null; then
local service="git-$cmd"
_git "$@"
else
local service="$cmd"
_normal
fi
}
# tell zsh to use the above completion functions for the special masked git commands
for cmd in $git_cmds; do
compdef _my_$cmd $cmd
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment