To get a prompt which indicates Git-branch in Zsh

前端 未结 9 1893
滥情空心
滥情空心 2020-12-04 09:12

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.

9条回答
  •  一个人的身影
    2020-12-04 09:34

    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:

    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:

    prompt

    Compared with git status -s:

    enter image description here

    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.

提交回复
热议问题