mac下使用iTerm2終端,安裝了zsh shell和oh-my-zsh配置,且使用主題agnoster,日常情況下都很好用沒有什么問題,直到進入一個WebRTC大型工程時執行任何命令都非常慢,猜測應該是命令提示符顯示了當前目錄的git的一些狀態,而這個工程的.git
太龐大了,所以讀取git狀態很慢。
網上查找解決辦法:
關閉讀取git信息
\$ git config --add oh-my-zsh.hide-dirty 1 \$ git config --add oh-my-zsh.hide-status 1
開啟則將1改為0。
但這個辦法相當于完全關閉了命令提示符的git顯示功能,無法知道當前git的一些信息,比如我只想知道當前git分支信息,這個信息使用git讀取應該是非常快的,而其他git動態信息才是影響速度的關鍵。
于是自己手動修改on-my-zsh的配置。
一開始修改$ZSH/lib/git.zsh
,但使用source ~/.zshrc
無法使修改生效。
最后發現git信息讀取應該是在當前主題agnoster中被配置。
解決辦法
修改$ZSH/themes/agnoster.zsh-theme
文件的prompt_git()
方法:
prompt_git() {
(( $+commands[git] )) || return
if [[ "$(git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]]; then
return
fi
local PL_BRANCH_CHAR
() {
local LC_ALL="" LC_CTYPE="en_US.UTF-8"
PL_BRANCH_CHAR=$'\ue0a0' #
}
local ref dirty mode repo_path
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
repo_path=$(git rev-parse --git-dir 2>/dev/null)
dirty=$(parse_git_dirty)
ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="? $(git rev-parse --short HEAD 2> /dev/null)"
if [[ -n $dirty ]]; then
prompt_segment yellow black
else
prompt_segment green $CURRENT_FG
fi
if [[ -e "${repo_path}/BISECT_LOG" ]]; then
mode=" <B>"
elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
mode=" >M<"
elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
mode=" >R>"
fi
#新增zsh.hide-status配置,不顯示一些動態信息,提高速度=================
if [[ "$(git config --get zsh.hide-status 2>/dev/null)" = 1 ]]; then
echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${mode}"
return
fi
#=================================================================
setopt promptsubst
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' get-revision true
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' stagedstr '?'
zstyle ':vcs_info:*' unstagedstr '●'
zstyle ':vcs_info:*' formats ' %u%c'
zstyle ':vcs_info:*' actionformats ' %u%c'
vcs_info
echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
fi
}
保存后,讓配置生效:
source ~/.zshrc
進入工程目錄開啟配置,只對當前git工程生效:
git config --add zsh.hide-status 1
shell飛快起來了~~~~