To get a prompt which indicates Git-branch in Zsh

前端 未结 9 1858
滥情空心
滥情空心 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:42

    If all your prompt looks fine but it does not work, e.g:

    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}%}$ "
    

    The PROMPT must set to a string quoted using single quotes instead of double quotes.

    PROMPT='%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m:%{$fg[yellow]%}%1~%{$reset_color%} %{%F{green}%}$(git_branch)%{%F{none}%}$ '
    
    0 讨论(0)
  • 2020-12-04 09:57

    A lot of these solutions seemed slow for me when mashing the return key, so here's an option that is basic and speedy:

    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}%} $ '
    

    You'll get a prompt that looks like this: ~/dev/project (feature-branch) $

    0 讨论(0)
  • 2020-12-04 09:58

    __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)'
    
    0 讨论(0)
提交回复
热议问题