zsh用了三四年,随着插件越来越多,启动tab越来越慢,基本上要1秒多,是时候优化一波啦。
优化记录:
1.将主题powerlevel9k升级至powerlevel10k
2.使用zinit加载插件
3.使用nvm、zsh-defer懒加载
1.升级主题
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k
# Replace ZSH_THEME="powerlevel9k/powerlevel9k" with ZSH_THEME="powerlevel10k/powerlevel10k
2.使用zinit加载插件
# 脚本自动安装 sh -c "$(curl -fsSL https://raw.githubusercontent.com/zdharma/zinit/master/doc/install.sh)" # 手动安装 git clone https://github.com/zdharma/zinit.git ~/.zinit/bin source ~/.zinit/bin/zinit.zsh # zinit使用 zinit light zsh-users/zsh-autosuggestions zinit light zdharma/fast-syntax-highlighting zinit light zsh-users/zsh-syntax-highlighting ...
3.使用懒加载
# 使用zsh-defer
git clone https://github.com/romkatv/zsh-defer.git ~/zsh-defer
source ~/zsh-defer/zsh-defer.plugin.zsh
# 使用zsh-nvm
git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm
# pyenv懒加载
git clone https://github.com/davidparsson/zsh-pyenv-lazy.git ~/.oh-my-zsh/custom/plugins/pyenv-lazy
4.查看优化后效果
# 从1s多优化到0.1s,确实快了很多
$ for i in $(seq 1 5); do /usr/bin/time /bin/zsh -i -c exit; done
0.13 real 0.05 user 0.07 sys
0.12 real 0.04 user 0.06 sys
0.12 real 0.04 user 0.06 sys
0.12 real 0.04 user 0.07 sys
0.12 real 0.04 user 0.06 sys
# 打出耗时操作
$ zmodload zsh/zprof
$ zprof |head -10
num calls time self name
-----------------------------------------------------------------------------------
1) 9 274242.36 30471.37 82.57% 274242.36 30471.37 82.57% compdump
2) 95 11949.68 125.79 3.60% 10541.34 110.96 3.17% _zsh_autosuggest_bind_widgets
3) 8 14616.86 1827.11 4.40% 9748.99 1218.62 2.94% bracketed-paste-magic
4) 9380 30610.04 3.26 9.22% 5708.55 0.61 1.72% _zsh_highlight_call_widget
5) 9165 9685.98 1.06 2.92% 5458.38 0.60 1.64% _zsh_highlight
6) 4294 108067.35 25.17 32.54% 3296.88 0.77 0.99% (anon)
7) 11 4554.74 414.07 1.37% 2662.07 242.01 0.80% _zsh_highlight_bind_widgets
8) 14 2654.74 189.62 0.80% 2654.74 189.62 0.80% file-to-clipboard
我的zsh配置,供参考:https://github.com/anzhihe/Efficient-office/blob/master/workspace/zshrc
参考: