DEV Community

loading...
Cover image for Supercharge Zsh Startup

Supercharge Zsh Startup

waylonwalker profile image Waylon Walker Originally published at waylonwalker.com ・4 min read

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

Discussion (4)

Collapse
maxdevjs profile image
maxdevjs

I am curious now... did you try your exact old, slow on wsl, configuration with a locally installed GNU/Linux?

I recently switched to zimfw and it is effectively lighter than oh-my-zsh.

Testing time zsh -c exit using most of oh-my-zsh features, a heavily festooned zsh configuration split in several files, etc versus no configuration at all returned the same result: basically zero.

Cold startup time of a terminal + zsh + oh-my-zsh is barely noticeable:

  • xterm around 0.2s
  • kitty around 0.3s

For nvim with plenty of plugins, configuration split in several files, etc the startup time is averagely under 0.2s . Cold startup time of a terminal + zsh + oh-my-zsh + nvim is between 0.3s and 0.4s

zimfw environment seems even faster.

I recently found that I can screw the configuration and reach a noticeable lag, but in very edge cases (example: executing the wrong script in the wrong startup file/sequence).

Note: tests done on very old hardware, but with SSD drives :)

Collapse
waylonwalker profile image
Waylon Walker Author

Yes I use the same config accross many machines running rhel, Debian, ubuntu, accross several cloud platforms. The only place that I really see this issue is inside wsl1. I assume that it is constrained by it's known terribly slow file io. Waiting patiently for wsl2 as there is no way for me to get a non windows main machine.

Collapse
henrikrudstrom profile image
henrik

Zplugin, now known as zinit has async loading of plugins, I have a super bloated setup with all bells an whistles but I still get my prompt in 0.2 seconds

Collapse
waylonwalker profile image
Waylon Walker Author

I've used mine on native linux systems and its crazy fast no matter how much you bloat it up. For some reason the bloat was really crippling wsl.

Forem Open with the Forem app