在Zsh中得到一个表示Git分支的提示 [英] To get a prompt which indicates Git-branch in Zsh

查看:1166
本文介绍了在Zsh中得到一个表示Git分支的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的提示在.zshrc中失败时,我单独运行以下代码。这表明我显然没有一个名为__git_ps1的程序。它不在MacPorts中。



#1



  PROMPT = $(__ git_ps1\ [\033 [1; 32m \](%s)\ [\033 [0m \])\ $$ 



#2



  PROMPT =$(__ git_ps1 (%s))\ $$ 



#3



 #获取分支的名称we在
上git_prompt_info(){
branch_prompt = $(__ git_ps1)
if [-n$ branch_prompt];然后
status_icon = $(git_status)
echo $ branch_prompt $ status_icon
fi
}

#显示字符,如果更改未决
git_status (){
如果current_git_status = $(git status | grep'添加到提交'2> / dev / null);然后
echo☠
fi
}
自动加载-U颜色
颜色
setopt prompt_subst
PROMPT ='
% 〜%{$ fg_bold [black]%} $(git_prompt_info)
→%{$ reset_color%}'

如何获得显示Git分支名称的提示?

解决方案

__ git_ps1 来自git-completion.bash。在zsh中,你可能必须提供你自己的函数来确定当前的目录git分支。有许多关于博客文章。 http://nullcreations.net/entries/general/zsh-prompt-to-show-git-branchrel =noreferrer> git提示符 for zsh。



您只需要:


  • 提供分支名称的函数
  • 启用提示(命令)替换

  • 将函数添加到提示中



例如

  git_prompt(){
ref = $(git symbolic-ref HEAD | cut -d'/'-f3)
echo $ ref

setopt prompt_subst
PS1 = $(git_prompt)%#
autoload -U promptinit
promptinit

更新:使用zsh vcs_info模块而不是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}%一%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:*'启用git cvs svn

#或使用pre_cmd,参见man zshcontrib
vcs_info_wrapper(){
vcs_info
if [-n$ vcs_info_msg_0_];然后
echo%{$ fg [gray]%} $ {vcs_info_msg_0 _}%{$ reset_color%} $ del
fi
}
RPROMPT = $'$(vcs_info_wrapper )'


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.

#1

PROMPT="$(__git_ps1 " \[\033[1;32m\] (%s)\[\033[0m\]")\$"$

#2

PROMPT="$(__git_ps1 " (%s)")\$"$

#3

# 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:

  • a function to provide the branch name
  • enable prompt (command) substitution
  • add the function to your prompt

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)'

这篇关于在Zsh中得到一个表示Git分支的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆