I run the following codes separately as my prompt unsuccessfully in .zshrc. This suggests me that apparently I do not have a program called __git_ps1. It is not in MacPorts.
PROMPT="$(__git_ps1 " \[\033[1;32m\] (%s)\[\033[0m\]")\$"$
PROMPT="$(__git_ps1 " (%s)")\$"$
# Get the name of the branch we are on
git_prompt_info() {
branch_prompt=$(__git_ps1)
if [ -n "$branch_prompt" ]; then
status_icon=$(git_status)
echo $branch_prompt $status_icon
fi
}
# Show character if changes are pending
git_status() {
if current_git_status=$(git status | grep 'added to commit' 2> /dev/null); then
echo "☠"
fi
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='
%~%{$fg_bold[black]%}$(git_prompt_info)
→ %{$reset_color%}'
How can you get a prompt which shows the name of a Git-branch?
__git_ps1
is from git-completion.bash. In zsh you probably have to provide your own function to determine the current directories git branch. There are quite a few blog posts about a git prompt for zsh.
You just need:
For example
git_prompt() {
ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
echo $ref
}
setopt prompt_subst
PS1=$(git_prompt)%#
autoload -U promptinit
promptinit
Update: use the zsh vcs_info module instead of git_prompt()
setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' actionformats \
'%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats \
'%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'
zstyle ':vcs_info:*' enable git cvs svn
# or use pre_cmd, see man zshcontrib
vcs_info_wrapper() {
vcs_info
if [ -n "$vcs_info_msg_0_" ]; then
echo "%{$fg[grey]%}${vcs_info_msg_0_}%{$reset_color%}$del"
fi
}
RPROMPT=$'$(vcs_info_wrapper)'
git_prompt is wrong, if you have a branch with / it does not work. Use
cut -d'/' -f3-
instead.Thanks!, this works. Can you please explain what those cryptic zstyle commands do?
@balki The
%F{n}
enable ANSI colorn
for foreground, and%f
disables foreground color. E.g., theformats
format%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}]%f
becomes(%s)-[%b]
if you ignore the colors. The%s
gets replaced by the vc system (e.g. git) and the%b
gets replaced by the current branch.@balki The
(sv[nk]|bzr)
subpattern on thebranchformat
restricts it tosvn
,svk
, andbzr
. It means that those systems should usebranch:revision
instead of the defaultbranch
. Because of theenable
line,bzr
is not supported, so thisbzr
restriction is "dead code". The first three lines are copied verbatim from the ZSH docs, but the last line was added by someone else, which probably explains the dead code.@balki The
actionformats
becomes(%s)-[%b|%a]
ignoring colors. This format is used during special actions, with%a
describing the action. This is the most useful feature IMHO: e.g. for git it tells when you are in the middle of a rebase (which I often forget about while trying to resolve merge conflicts).