Skip to content

Instantly share code, notes, and snippets.

@murilogteixeira
Forked from bamanzi/zsh-git-prompt.sh
Created January 18, 2021 12:51
Show Gist options
  • Save murilogteixeira/df2b48be4a3b4a22c4e31e9944402f9d to your computer and use it in GitHub Desktop.
Save murilogteixeira/df2b48be4a3b4a22c4e31e9944402f9d to your computer and use it in GitHub Desktop.
zsh: show git repo name, branch in right prompt
# although StackOverflow has this answer http://stackoverflow.com/a/1128583
# but that code is unreadable, thus I have this (also based on a SO anower: http://stackoverflow.com/a/1128721 )
# get the name of the branch we are on
_git_repo_name() {
gittopdir=$(git rev-parse --git-dir 2> /dev/null)
if [[ "foo$gittopdir" == "foo.git" ]]; then
echo `basename $(pwd)`
elif [[ "foo$gittopdir" != "foo" ]]; then
echo `dirname $gittopdir | xargs basename`
fi
}
_git_branch_name() {
git branch 2>/dev/null | awk '/^\*/ { print $2 }'
}
_git_is_dirty() {
git diff --quiet 2> /dev/null || echo '*'
}
autoload -U colors
colors
setopt prompt_subst
RPROMPT='$(_git_repo_name) $(_git_branch_name) $(_git_is_dirty)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment