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.
我单独运行以下代码,因为我的提示在.zshrc中失败。这表明我显然没有一个名为__git_ps1的程序。它不在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分支名称的提示?
5 个解决方案
#1
__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.
__git_ps1来自git-completion.bash。在zsh中,您可能必须提供自己的函数来确定当前目录git branch。关于zsh的git提示,有不少博客文章。
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()
更新:使用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)'
#3
ko-dos's answer is great, but I prefer a slightly more git aware prompt than the one he uses. Specifically, I like staged, unstaged, and untracked file notifications in the prompt itself. Using his answer and the zsh vcs_info examples, I came up with the following:
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%# '
This creates a prompt that mimics the colorized output of git status -s
(which can be configured in your .gitconfig
file). A picture is perhaps most helpful here:
这会创建一个模仿git status -s(可以在.gitconfig文件中配置)的彩色输出的提示。这张照片在这里可能最有帮助:
Compared with git status -s
:
与git status -s相比:
If you don't like colorized output, or would prefer some other character or prompt construction, just change the stagedstr
, unstagedstr
, and hook_com[unstaged]
values in the above code.
如果您不喜欢彩色输出,或者更喜欢其他字符或提示构造,只需在上面的代码中更改stagedstr,unstagedstr和hook_com [unstaged]值。
#4
Thank you for the links!
谢谢你的链接!
I made the following prompt based on them
我根据它们做了以下提示
# get the name of the branch we are on
git_prompt_info() {
git branch | awk '/^\*/ { print $2 }'
}
get_git_dirty() {
git diff --quiet || echo '*'
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'
RPROMPT='%{$fg[green]%}%1(j.%j.)'
Please, suggest any improvements.
请提出任何改进建议。
#5
I just redid mine since we have long branch names at work. This one will truncate with an ellipsis if it's more than 35 characters.
我刚刚重新开采,因为我们有很长的分支名称。如果超过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)$'
(I'm embarrassed at how proud I am of this.)
(我为此感到骄傲感到尴尬。)
#1
__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.
__git_ps1来自git-completion.bash。在zsh中,您可能必须提供自己的函数来确定当前目录git branch。关于zsh的git提示,有不少博客文章。
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()
更新:使用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)'
#2
Here is an extended git prompt for zsh: zsh-git-prompt.
这是zsh:zsh-git-prompt的扩展git提示符。
#3
ko-dos's answer is great, but I prefer a slightly more git aware prompt than the one he uses. Specifically, I like staged, unstaged, and untracked file notifications in the prompt itself. Using his answer and the zsh vcs_info examples, I came up with the following:
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%# '
This creates a prompt that mimics the colorized output of git status -s
(which can be configured in your .gitconfig
file). A picture is perhaps most helpful here:
这会创建一个模仿git status -s(可以在.gitconfig文件中配置)的彩色输出的提示。这张照片在这里可能最有帮助:
Compared with git status -s
:
与git status -s相比:
If you don't like colorized output, or would prefer some other character or prompt construction, just change the stagedstr
, unstagedstr
, and hook_com[unstaged]
values in the above code.
如果您不喜欢彩色输出,或者更喜欢其他字符或提示构造,只需在上面的代码中更改stagedstr,unstagedstr和hook_com [unstaged]值。
#4
Thank you for the links!
谢谢你的链接!
I made the following prompt based on them
我根据它们做了以下提示
# get the name of the branch we are on
git_prompt_info() {
git branch | awk '/^\*/ { print $2 }'
}
get_git_dirty() {
git diff --quiet || echo '*'
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'
RPROMPT='%{$fg[green]%}%1(j.%j.)'
Please, suggest any improvements.
请提出任何改进建议。
#5
I just redid mine since we have long branch names at work. This one will truncate with an ellipsis if it's more than 35 characters.
我刚刚重新开采,因为我们有很长的分支名称。如果超过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)$'
(I'm embarrassed at how proud I am of this.)
(我为此感到骄傲感到尴尬。)