# 为何启动 iTerm2时 会卡一下
一直发现 zsh
启动时会卡一下,所以在找这个原因,然后发现其实是以下这个代码在作祟:
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
可以看到 这段代码是用来加载 nvm
的,把这段移除就会有明显的速度提升,但移除的话我的 nvm
就用不了了,该怎么办呢?经过一顿 google 后,发现了 zinit
(opens new window) 这个插件,可以看到描述就是 🌻 Flexible and fast ZSH plugin manager ,这不就解决了当前的问题了吗,哈哈哈哈
又因为我之前是用 ohmyzsh
(opens new window),但又不想抛弃其中的的一些好用的插件,那该怎么办呢?
答案就是 zinit
支持加载 omz
的插件,这不万事大吉吗!
# 卸载 omz
,改为 zinit
支持
安装 sh -c "$(curl -fsSL https://git.io/zinit-install)"
会初始化一个 .zshrc
文件,并写入如下代码:
### Added by Zinit's installer
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
print -P "%F{33} %F{220}Installing %F{33}ZDHARMA-CONTINUUM%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
command mkdir -p "$HOME/.local/share/zinit" && command chmod g-rwX "$HOME/.local/share/zinit"
command git clone https://github.com/zdharma-continuum/zinit "$HOME/.local/share/zinit/zinit.git" && \
print -P "%F{33} %F{34}Installation successful.%f%b" || \
print -P "%F{160} The clone has failed.%f%b"
fi
source "$HOME/.local/share/zinit/zinit.git/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit
# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for \
zdharma-continuum/zinit-annex-as-monitor \
zdharma-continuum/zinit-annex-bin-gem-node \
zdharma-continuum/zinit-annex-patch-dl \
zdharma-continuum/zinit-annex-rust
### End of Zinit's installer chunk
之后就是我们自己添加自定义代码了:
zinit light romkatv/zsh-defer
# 加载 onmyzsh 插件
zsh-defer zinit snippet OMZ::lib/history.zsh
zsh-defer zinit snippet OMZ::lib/key-bindings.zsh
zsh-defer zinit snippet OMZ::lib/git.zsh
zsh-defer zinit snippet OMZ::plugins/sudo/sudo.plugin.zsh
# 加载插件
zsh-defer zinit wait'1' lucid for \
zsh-users/zsh-autosuggestions \
agkozak/zsh-z \
zsh-users/zsh-completions \
zdharma-continuum/fast-syntax-highlighting
export_init_fun() {
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
export PATH="/Users/mac/Yinge/cli/bin:$PATH"
autoload -U compinit; compinit
export PATH="$HOME/.ycli/release:$PATH"
}
zsh-defer export_init_fun
init_fun() {
autoload -U add-zsh-hook
}
# 切换目录如果有 .nvmrc 文件,根据内容切换 node 版本
load_nvmrc() {
if [[ -f .nvmrc && -r .nvmrc ]]; then
nvm use
fi
}
add-zsh-hook chpwd load_nvmrc
alias ..="cd .."
eval "$(starship init zsh)"
可以看到其中还有一个 zsh-defer
(opens new window) 插件,这个就是用来延迟执行 zsh 命令的
所以根据这个 我们可以 延迟执行 nvm
脚本: zsh-defer export_init_fun
可以看到 最后一句代码:eval "$(starship init zsh)"
,这是一个主题框架,我看到 zinit
中有如下代码 Prompt(Theme) Example (opens new window):
# Load starship theme
zinit ice as"command" from"gh-r" \ # `starship` binary as command, from github release
atclone"./starship init zsh > init.zsh; ./starship completions zsh > _starship" \ # starship setup at clone(create init.zsh, completion)
atpull"%atclone" src"init.zsh" # pull behavior same as clone, source init.zsh
zinit light starship/starship
但最后执行不成功,可能是我之前用 brew install starship
的原因 🤔 ?
经过一番调教后 zsh
启动速度 嗖嗖的 哈哈哈哈哈哈
# 分析
- 对 Zsh 启动时间进行测量:
time zsh -i -c exit
- 评估 Zsh 冷启动过程中的时间使用: 可以使用
zprof
,这个 zsh 自带的工具- 在 .zshrc 的最开头新增一行并写入 zmodload zsh/zprof;
- 在 .zshrc 文件末尾添加一行再写入 zprof;
- 保存 .zshrc 再重启我们的 Zsh Shell(关闭再打开终端);