我单独运行以下代码,因为我的提示在.zshrc中失败.这表明我显然没有一个名为__git_ps1的程序.它不在MacPorts中.
PROMPT="$(__git_ps1 " \[\033[1;32m\] (%s)\[\033[0m\]")\$"$
Run Code Online (Sandbox Code Playgroud)
PROMPT="$(__git_ps1 " (%s)")\$"$
Run Code Online (Sandbox Code Playgroud)
# 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%}'
Run Code Online (Sandbox Code Playgroud)
你怎么能得到一个显示Git分支名称的提示?
ko-*_*dos 65
__git_ps1来自git-completion.bash.在zsh中,您可能必须提供自己的函数来确定当前目录git branch.关于zsh 的git提示,有不少博客文章.
您只需要:
例如
git_prompt() {
ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
echo $ref
}
setopt prompt_subst
PS1=$(git_prompt)%#
autoload -U promptinit
promptinit
Run Code Online (Sandbox Code Playgroud)
更新:使用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}%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)'
Run Code Online (Sandbox Code Playgroud)
Oli*_*ier 34
这是zsh:zsh-git-prompt的扩展git 提示符.

Chr*_*her 26
ko-dos的答案很棒,但我更喜欢一个比他使用的更敏捷的提示.具体来说,我喜欢提示本身中的分阶段,未分阶段和未跟踪的文件通知.使用他的答案和zsh vcs_info示例,我想出了以下内容:
setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' stagedstr 'M'
zstyle ':vcs_info:*' unstagedstr 'M'
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' actionformats '%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats \
'%F{5}[%F{2}%b%F{5}] %F{2}%c%F{3}%u%f'
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked
zstyle ':vcs_info:*' enable git
+vi-git-untracked() {
if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
[[ $(git ls-files --other --directory --exclude-standard | sed q | wc -l | tr -d ' ') == 1 ]] ; then
hook_com[unstaged]+='%F{1}??%f'
fi
}
precmd () { vcs_info }
PROMPT='%F{5}[%F{2}%n%F{5}] %F{3}%3~ ${vcs_info_msg_0_} %f%# '
Run Code Online (Sandbox Code Playgroud)
这会创建一个模仿彩色输出的提示git status -s(可以在.gitconfig文件中配置).这张照片在这里可能最有帮助:

与git status -s:相比:

如果你不喜欢彩色输出,或宁愿其它字符或提示建设,只是改变了stagedstr,unstagedstr和hook_com[unstaged]在上面的代码值.
这已经有很多很好的答案,对于那些不想使用除了 configzsh和git.
然而,我发现自己想要的只是
working-directory (git-branch-color-if-uncommitted) % 24h-timestamp
Run Code Online (Sandbox Code Playgroud)
git_branch_test_color() {
local ref=$(git symbolic-ref --short HEAD 2> /dev/null)
if [ -n "${ref}" ]; then
if [ -n "$(git status --porcelain)" ]; then
local gitstatuscolor='%F{red}'
else
local gitstatuscolor='%F{green}'
fi
echo "${gitstatuscolor} (${ref})"
else
echo ""
fi
}
setopt PROMPT_SUBST
PROMPT='%9c$(git_branch_test_color)%F{none} %# '
# add 24h time the right side
RPROMPT='%D{%k:%M:%S}'
Run Code Online (Sandbox Code Playgroud)
这是由
$()允许通过设置包含函数 ( ) PROMPT_SUBST(感谢Phil 2017 年的回答!)git_branch_test_color来调用git分支 ref ..以及如果当前目录是 git 存储库的话这是否成功(测试 stdout)symbolic-ref!)
""git status --porcelain用于确定是否有任何文件已更改或新增/删除
请注意,所有此类提示在已安装的目录上可能会很慢(尤其是通过sshfs)。如果有这种经验,我建议尽可能对这些目录进行特殊处理(也许创建一个新的 shell 函数来检查 cd 是否在您的集合中)或始终安装在同一路径中并忽略它(例如vcs_info)
我只是重做了我的,因为我们有很长的分支名称在工作。如果它超过 35 个字符,这个将被省略号截断。
parse_git_branch() {
git_status="$(git status 2> /dev/null)"
pattern="On branch ([^[:space:]]*)"
if [[ ! ${git_status} =~ "(working (tree|directory) clean)" ]]; then
state="*"
fi
if [[ ${git_status} =~ ${pattern} ]]; then
branch=${match[1]}
branch_cut=${branch:0:35}
if (( ${#branch} > ${#branch_cut} )); then
echo "(${branch_cut}…${state})"
else
echo "(${branch}${state})"
fi
fi
}
setopt PROMPT_SUBST
PROMPT='%{%F{blue}%}%9c%{%F{none}%}$(parse_git_branch)$'
Run Code Online (Sandbox Code Playgroud)
(我为我为此感到多么自豪而感到尴尬。)
当我混入回车键时,许多这些解决方案对我来说似乎很慢,因此这是一个基本且快速的选项:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
setopt PROMPT_SUBST
PROMPT='%9c%{%F{green}%}$(parse_git_branch)%{%F{none}%} $ '
Run Code Online (Sandbox Code Playgroud)
您会看到如下提示:
~/dev/project (feature-branch) $
如果您的所有提示看起来不错但不起作用,例如:
function git_branch(){
ref=$(git symbolic-ref --short --quiet HEAD 2>/dev/null)
if [ -n "${ref}" ]; then
echo "(""$ref"")"
fi
}
setopt PROMPT_SUBST
PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m:%{$fg[yellow]%}%1~%{$reset_color%} %{%F{green}%}$(git_branch)%{%F{none}%}$ "
Run Code Online (Sandbox Code Playgroud)
PROMPT 必须设置为使用单引号而不是双引号引起来的字符串。
PROMPT='%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m:%{$fg[yellow]%}%1~%{$reset_color%} %{%F{green}%}$(git_branch)%{%F{none}%}$ '
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48627 次 |
| 最近记录: |