解决oh-my-zsh中git分支显示乱码问题

oh-my-zsh显示github分支时,如果当前文件夹不是git仓库,它就会显示乱码。倒腾了好几个小时终于弄清楚是oh-my-zsh中函数”git_prompt_info“的锅,然后又花了半个多小时调代码,现在总算像个人了!!

先看效果:

实现步骤:

  1. 进入 ~/.oh-my-zsh/lib 文件夹
  2. 编辑 git.zsh,找到git_prompt_info函数:

    function git_prompt_info() {
      local ref
      if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
        ref=$(command git symbolic-ref HEAD 2> /dev/null) || 
        if [[ -n $ref ]]; then
          ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
        fi
        if [[ -n ${ref#refs/heads/} ]]; then
          echo " ${ref#refs/heads/}"
        fi
      fi
    }

    这里修改显示逻辑,如果当前文件夹属于git仓库,就显示分支,否则什么也不显示。

  3. 进入 ~/.oh-my-zsh/thems 文件夹,编辑robbyrussell.zsh-theme

    local ret_status="%(?:%{$fg_bold[green]%}➜:%{$fg_bold[red]%}➜)"
    PROMPT='${ret_status} %{$fg[cyan]%}%c%{$fg_bold[blue]%}$(git_prompt_info) %{$reset_color%}$ '
    ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}"
    ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
    ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
    ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
    

    这里是修改prompt格式,直接拷贝进去就好,不要漏空格,否则会很丑。

 大功告成~! 

原文地址:https://www.cnblogs.com/Rhythm-/p/11337006.html