ZSH에서 Git-Branch를 나타내는 프롬프트를 얻으려면
문제
.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%}'
Git-Branch의 이름을 보여주는 프롬프트를 어떻게 얻을 수 있습니까?
해결책
__git_ps1
git-completion.bash에서 나온 것입니다. ZSH에서는 현재 디렉토리 GIT 브랜치를 결정하기 위해 자신의 기능을 제공해야 할 것입니다. 꽤 많은 것이 있습니다 블로그 게시물 ~에 관하여 git 프롬프트 ZSH를 위해.
당신은 그냥 필요합니다 :
- 지점 이름을 제공하는 함수
- 프롬프트 (명령) 대체를 활성화합니다
- 프롬프트에 함수를 추가하십시오
예를 들어
git_prompt() {
ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
echo $ref
}
setopt prompt_subst
PS1=$(git_prompt)%#
autoload -U promptinit
promptinit
업데이트 : git_prompt () 대신 zsh vcs_info 모듈을 사용하십시오.
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 프롬프트입니다. Zsh-Git-Prompt.
Ko-Dos의 대답은 훌륭하지만, 나는 그가 사용하는 것보다 약간 더 많은 git 인식 프롬프트를 선호합니다. 구체적으로, 나는 프롬프트 자체에서 무대, 무대 및 무리 및 추적되지 않은 파일 알림을 좋아합니다. 그의 대답과 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%# '
이것은 색상화 된 출력을 모방하는 프롬프트를 만듭니다. git status -s
(귀하의 구성 할 수 있습니다 .gitconfig
파일). 사진이 가장 도움이 될 것입니다.
비교 git status -s
:
색상화 된 출력을 좋아하지 않거나 다른 문자 나 프롬프트 구성을 선호하는 경우 stagedstr
, unstagedstr
, 그리고 hook_com[unstaged]
위 코드의 값.
리턴 키를 으깨면 많은 솔루션이 느리게 보였으므로 기본적이고 빠른 옵션이 있습니다.
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}%} $ '
다음과 같이 보이는 프롬프트가 나타납니다.~/dev/project (feature-branch) $
링크에 감사드립니다!
나는 그들을 기반으로 다음과 같은 프롬프트를 만들었습니다
# 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.)'
개선 사항을 제안하십시오.
우리는 직장에서 긴 지점 이름이 있기 때문에 단지 내 것을 다시 만들었습니다. 이것은 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)$'
(내가 이것에 대해 얼마나 자랑스러워하는지 당황 스럽습니다.)