Supercharge Zsh Startup

Waylon Walker - Jan 12 '20 - - Dev Community

Photo by Max LaRochelle on Unsplash

I have been using oh-my-zsh successfully for about 2 years now. But lately my startup time has been really bothersome. It has grown to the point where it was taking about 5.5s to startup a shell! This is ok if I am going to spend some time in here for awhile and do some work that benefits from all of the auto completions, plugins, and shortcuts that oh-my-zsh brings. But to only jump in to run a handful of commands is infuriating.

📑 My Setup

I believe the real issue is io speed on wsl. I have some remote servers with similar configs that are 10x faster or more, loading in 100s of milliseconds rather than seconds. Sourcing all of the individual plugin files are just too much for it.

  • wsl (Debian)
  • zsh
  • neovim

💨 How Fast can it be

Quick side note: your zsh config is controled by your ~/.zshrc file. This file can source other files, load plugins, or run literally anything.

Time the initial time

time zsh -c exit
Enter fullscreen mode Exit fullscreen mode

Move your ~/.zshrc config file.

mv ~/.zshrc ~/.zshrc-back
Enter fullscreen mode Exit fullscreen mode

Time the fastest startup possible with nothing in your ~/.zshrc config file.

time zsh -c exit
Enter fullscreen mode Exit fullscreen mode

Move your ~/.zshrc back

mv ~/.zshrc-back ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

🕵️‍♂️Profile your startup time

It is possible to profile your zsh startup time by adding zmodload zsh/zprof to the start of your ~/.zshrc file and zprof at the end. This was unsuccessful for me. I ended up just backing up ~/.zshrc file, then deleting half of it to see where the hot spots were. I found that two places that were really hot for me. One I was inadvertently setting git and npm settings every time that didn't need to be set every time. That was an easy 2s gain. Another easy 3s gain was removing oh-my-zsh.

This was unsuccessful for me

# ~/.zshrc
zmodload zsh/zprof
...
..
.
zprof
Enter fullscreen mode Exit fullscreen mode

😭But I really like oh-my-zsh

without all the bells and whistle that oh-my-zsh provided zsh became lightning fast to load, but incredibly boring. It was also very painful to manually type out everything that it autocompleted or aliased all the time. Next I headed down a path to get all of that functionality back without sacrificing load time.

Without oh-my-zsh, zsh became incredibly boring.

🔌 Plugin Managers

Plugin

Photo by Steve Johnson on Unsplash

  • oh-my-zsh
  • zplugin
  • zgen

There are a number of plugin managers for zsh, I tried each of the ones listed above, but found that as I approached a nice setup that I liked the load time would creep up above the 2s mark each time. I would turn certain plugins on and off, try different plugin managers, before realizing that I had spent enough time on this problem and it was going to be time to settle on fast startup or functionality.

⚖ Finding Balance

Semi-lazy loading

balancing rocks

Photo by Jeppe Hove Jensen on Unsplash

After struggling to get all of the features I wanted with a fast load time, I decided to only load what I needed upon startup. Next I created a simple alias that loads in zgen and all of the plugins I want. By doing this I get two main benefits. Obviously I get a faster starup time by loading less. I got my startup time down to about 0.25s.

# ~/.zshrc
p () {
zgen load zsh-users/zsh-autosuggestions
....
..
.
}
Enter fullscreen mode Exit fullscreen mode

⚡ Fast Loading

I really like the fast startup time, because sometimes I am only loading up zsh to run a handful of commands that don't need much in the way of plugins

simple commands that need 💨 blazing start speed

any single easy to type command, these are my common commands that I will open a terminal in my editor and just need to run quick.

  • vim
  • git add . && git commit && git push
  • sh my_script.sh
  • make build
  • bake build
  • pytest
  • gatsby develop
  • npm i
  • npm update
  • pip install
  • ipython

The second benefit was that I can continue typing while plugins are loading.

typing command while loading plugins

I can type commands while my plugins are loading. This gets me one step further in parallel to loading plugins.

>Prompt

After removing oh-my-zsh the first thing that I missed was the themes that it provided. I went through a number of them and the one that seemed to have the smallest effect on performance and everything I needed was starship. It's a really fast prompt written in rust. The biggest thing that I needed to have that other prompts were missing was conda environments. I live much of my work life running python from various conda environments and it is crucial that I can see what environment I am in at all times.

My Starship Prompt

💰 Bonus

I applied the same logic to neovim and achieved similar results. Again it just had too many plugins loading on startup for simple tasks. I ended up taking a shortcut and load any heavy plugins upon NerdTreeToggle. I dont really use NERDTree that much, but when I do its for more than just a quick edit.

# ~/.config/nvim/init.vim
...
Plug 'valloric/youcompleteme', {'do': './install.py', 'on': 'NERDTreeToggle'}
...
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .