前言
终于忍受不了越来越慢的 zsh 启动速度,优化了一下 zsh 的启动速度。
正文
环境
- iTerm2 3.3.11
- zsh 5.8
- oh my zsh(好像没版本号 commit 5ffc0d036c587741fd25092e7809dad2b00b3677)
初始配置
.zshrc
配置文件如下:
export ZSH=/Users/woodenrobot/.oh-my-zsh
ZSH_THEME="ys"
plugins=(
sudo
z
zsh_reload
safe-paste
extract
history-substring-search
colored-man-pages
git
history
tmux
# 第三方插件
zsh-autosuggestions
zsh-syntax-highlighting
)
source $ZSH/oh-my-zsh.sh
# User configuration
# Bind key
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
# 语言配置
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
# pyenv
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
# nvm
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
# virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
其中 zsh-syntax-highlighting
和 zsh-autosuggestions
是通过下列方式安装:
$ git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
通过配置文件可以看出,我安装了几个 oh my zsh 自带的插件以及 pyenv
、nvm
、virtualenvwrapper
,安装插件以及其他程序的启动脚本是需要耗费时间的,测试一下 此时 zsh
的启动速度。
$ time zsh -i -c exit
测了三次启动时间如下:
1.54 real 0.78 user 0.73 sys
1.55 real 0.80 user 0.72 sys
1.83 real 0.83 user 0.80 sys
优化
为了提高启动速度,先把 pyenv
、nvm
和virtualenvwrapper
程序改为 lazyload
,也就是不在 zsh 启动时就启动只在使用它们的时候启动。
nvm 优化
使用 zsh-nvm
插件实现 nvm
的 lazyload。
- 下载插件:
$ git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm
- 然后在
.zshrc
文件中开启插件
plugins=(
...
zsh-nvm
)
- 删除
.zshrc
原有的nvm
启动部分并开启zsh-nvm
的 lazyload,加入:
# zsh-nvm lazy load
export NVM_LAZY_LOAD=true
pyenv 优化
- 下载插件:
$ git clone https://github.com/davidparsson/zsh-pyenv-lazy.git ~/.oh-my-zsh/custom/plugins/pyenv-lazy
- 然后在
.zshrc
文件中开启插件
plugins=(
...
pyenv-lazy
)
- 删除
.zshrc
原有的pyenv
启动部分
virtualenvwrapper 优化
- 删除原有的
virtualenvwrapper
启动部分; - 使用下列 lazyload:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
source /usr/local/bin/virtualenvwrapper_lazy.sh
完整配置
export ZSH=/Users/woodenrobot/.oh-my-zsh
ZSH_THEME="ys"
plugins=(
sudo
z
zsh_reload
safe-paste
extract
history-substring-search
colored-man-pages
git
history
tmux
# 第三方插件
zsh-autosuggestions
zsh-syntax-highlighting
pyenv-lazy
zsh-nvm
)
source $ZSH/oh-my-zsh.sh
# User configuration
# Bind key
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
# 语言配置
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
# virtualenvwrapper lazy load
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
source /usr/local/bin/virtualenvwrapper_lazy.sh
# zsh-nvm lazy load
export NVM_LAZY_LOAD=true
优化完了测试一下现在的启动速度,老规矩测三次:
0.30 real 0.16 user 0.11 sys
0.26 real 0.14 user 0.10 sys
0.28 real 0.15 user 0.11 sys
效果感人!!!效果感人!!!效果感人!!!
歪门邪道
zsh 启动速度上来了但是新建一个 iTerm2 tab 好像还是有点慢做不到秒开。网上看到一个不知道什么原理的设置:
进入 iTerm2 的偏好设置里,在 Profiles 里编辑你的配置,在配置右侧的 General 选项卡里,Command 里选择为 Command,然后里边写入 /usr/bin/login -pfq xxx 其中 xxx 是你的用户名。
按照这个配置好,感觉是有那么一点点更快了(更慢了)?总之在我这里改动带来的体验并没有那么大,大家有兴趣可以去试试。
参考
- GitHub - lukechilds/zsh-nvm: Zsh plugin for installing, updating and loading nvm
- GitHub - davidparsson/zsh-pyenv-lazy: A zsh plugin for lazy loading of pyenv
- Installation — virtualenvwrapper 5.0.1.dev2 documentation
- iTerm 2、Terminal 启动加速
- 让 iTrem 2 + zsh 启动不再等待! | 落格博客zsh-users/zsh-autosuggestions让 iTrem 2 + zsh 启动不再等待! | 落格博客