oh-my-zsh顯示github分支時,如果當前文件夾不是git倉庫,它就會顯示亂碼。倒騰了好幾個小時終於弄清楚是oh-my-zsh中函數”git_prompt_info“的鍋,然后又花了半個多小時調代碼,現在總算像個人了!!
先看效果:
實現步驟:
- 進入 ~/.oh-my-zsh/lib 文件夾
- 編輯 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倉庫,就顯示分支,否則什么也不顯示。
- 進入 ~/.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格式,直接拷貝進去就好,不要漏空格,否則會很丑。
大功告成~!