作为一个伪文艺程序员,在ubuntu上使用zsh搭配OMG的插件oh-my-zsh,小日子曾经过的滋滋润润,各种auto-completion让我基本上爱上Tab键了。后来换了台旧MBP,每次跑到项目文件夹下git checkout feat (tab)就傻了,出去泡一杯咖啡回来shell还在哼哧哼哧地忙活着,分分钟有种再手贱<tab>就剁掉的冲动。

有位仁兄找到了问题的解决方法,原来作恶的是~/.oh-my-zsh/lib/git.zsh中的git_prompt_info() 调用的parse_git_dirty()函数。

# get the name of the branch we are on
function git_prompt_info() {
  if [[ "$(git config --get oh-my-zsh.hide-status)" != "1" ]]; then
    ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
    ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
    echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
  fi
}

# Checks if working tree is dirty
parse_git_dirty() {
  local SUBMODULE_SYNTAX=''
  local GIT_STATUS=''
  local CLEAN_MESSAGE='nothing to commit (working directory clean)'
  if [[ "$(command git config --get oh-my-zsh.hide-status)" != "1" ]]; then
    if [[ $POST_1_7_2_GIT -gt 0 ]]; then
          SUBMODULE_SYNTAX="--ignore-submodules=dirty"
    fi
    if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then
        GIT_STATUS=$(command git status -s ${SUBMODULE_SYNTAX} -uno 2> /dev/null | tail -n1)
    else
        GIT_STATUS=$(command git status -s ${SUBMODULE_SYNTAX} 2> /dev/null | tail -n1)
    fi
    if [[ -n $GIT_STATUS ]]; then
      echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
    else
      echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
    fi
  else
    echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  fi
}

他贴出的gist是这样的:

function git_prompt_info() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$ZSH_THEME_GIT_PROMPT_SUFFIX"
}

其实sf上早有人问过这问题,解决方案也早已存在于oh-my-zsh的标配插件中了。直接在~/.zshrc中修改下调用插件就好

plugins=(gitfast) # 原来是git

看看.oh-my-zsh/plugins/gitfast.plugin.zsh里的写法

dir=$(dirname $0)
source $dir/../git/git.plugin.zsh
source $dir/git-prompt.sh

function git_prompt_info() {
  dirty="$(parse_git_dirty)"
  __git_ps1 "${ZSH_THEME_GIT_PROMPT_PREFIX//\%/%%}%s${dirty//\%/%%}${ZSH_THEME_GIT_PROMPT_SUFFIX//\%/%%}"
}

没有了parse_git_dirty,我们就看不到zsh 中git repo名字旁边提示当前分支是否dirty的小勾勾“✔”和小叉叉“✗”,但是人生体验流畅了不只十个数量级啊。比起肆意的人生,OOXX算什么呢!!!(话题没有跑偏没有跑偏(;¬_¬) )

从此<tab>键又能肆意飞翔了。