惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
Y
Y Combinator Blog
博客园 - Franky
D
Docker
B
Blog RSS Feed
M
MIT News - Artificial intelligence
雷峰网
雷峰网
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
S
SegmentFault 最新的问题
GbyAI
GbyAI
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
B
Blog
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News

WoodenRobot's Blog

Mac OS 系统无法双击使用 Chrome 浏览器打开 HTML 文件 群晖 SSH 公钥免密登录 群晖 Moments 人物识别一键修复解决方案 获取联通友华 PT952G 光猫超级密码和宽带拨号账号密码 去除 iTerm2 下 Tmux 复制模式警告 优酷路由宝 YK-L1c 和 YK-L1 刷入 Breed 不死和 hiboy Padavan 固件 Zoom 直播分享 Awesome pipeline 录像和资料下载 Python 使用 Redis 实现分布式锁 [转]Python 相对导入与绝对导入 Mac 使用 Brew 升级 openssl@1.1 问题 Linux通过源码编译安装 Python3.8 家庭网络漫游指南 修复黑群晖 Moments 1.3.3-0700 版本人物识别不能使用问题 TOTOLINK A3004NS 国行刷入 Breed 不死和 hiboy Padavan 固件 Nginx 搭建 Google 镜像站 中兴 ZXHN F677V2 光猫改桥接 [转] Python 的 Buffer 机制 群晖 Video Station 支持 DTS 和 eac3 解决方案 浏览器输入 URL 后的历程
优化 oh my zsh 启动速度
本文作者: WoodenRobot · 2020-06-23 · via WoodenRobot's Blog

前言

终于忍受不了越来越慢的 zsh 启动速度,优化了一下 zsh 的启动速度。

正文

环境

  • iTerm2 3.3.11
  • zsh 5.8
  • oh my zsh(好像没版本号 commit 5ffc0d036c587741fd25092e7809dad2b00b3677)

初始配置

.zshrc 配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

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




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"


export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"


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"


export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

其中 zsh-syntax-highlightingzsh-autosuggestions 是通过下列方式安装:

1
2
3
$ 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 自带的插件以及 pyenvnvmvirtualenvwrapper,安装插件以及其他程序的启动脚本是需要耗费时间的,测试一下 此时 zsh 的启动速度。

1
$ \time zsh -i -c exit

测了三次启动时间如下:

1
2
3
4
5
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

优化

为了提高启动速度,先把 pyenvnvmvirtualenvwrapper程序改为 lazyload,也就是不在 zsh 启动时就启动只在使用它们的时候启动。

nvm 优化

使用 zsh-nvm 插件实现 nvm 的 lazyload。

  • 下载插件:

    1
    2
    $ git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm

  • 然后在 .zshrc 文件中开启插件

    1
    2
    3
    4
    plugins=(
    ...
    zsh-nvm
    )
  • 删除 .zshrc 原有的 nvm 启动部分并开启 zsh-nvm 的 lazyload,加入:

    1
    2

    export NVM_LAZY_LOAD=true

pyenv 优化

  • 下载插件:

    1
    $ git clone https://github.com/davidparsson/zsh-pyenv-lazy.git ~/.oh-my-zsh/custom/plugins/pyenv-lazy
  • 然后在 .zshrc 文件中开启插件

    1
    2
    3
    4
    plugins=(
    ...
    pyenv-lazy
    )
  • 删除 .zshrc 原有的 pyenv 启动部分

virtualenvwrapper 优化

  • 删除原有的 virtualenvwrapper 启动部分;
  • 使用下列 lazyload:
    1
    2
    3
    4
    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

完整配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

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




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"


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 NVM_LAZY_LOAD=true

优化完了测试一下现在的启动速度,老规矩测三次:

1
2
3
4
5
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 是你的用户名。

按照这个配置好,感觉是有那么一点点更快了(更慢了)?总之在我这里改动带来的体验并没有那么大,大家有兴趣可以去试试。

参考

  1. GitHub - lukechilds/zsh-nvm: Zsh plugin for installing, updating and loading nvm
  2. GitHub - davidparsson/zsh-pyenv-lazy: A zsh plugin for lazy loading of pyenv
  3. Installation — virtualenvwrapper 5.0.1.dev2 documentation
  4. iTerm 2、Terminal 启动加速
  5. 让 iTrem 2 + zsh 启动不再等待! | 落格博客