zsh starts incredibly slowly

64

38

ZSH takes about a second and a half from creating a new terminal window to being ready. I'm pretty sure that the culprit is compinit.

I haven't been able to find good documentation on compinit, but it looks like it should be caching all of the necessary things in some file like .zcompdump.

Any tricks on speeding it up?

Eli

Posted 2011-01-24T02:30:09.007

Reputation: 743

Answers

33

oh-my-zsh was taking about 1.5 seconds to start up on my laptop. I wrote up some of the steps I took to get that down to about 0.25 seconds.

Another kind soul summarized the steps required to integrate my changes into your copy of oh-my-zsh.

The biggest problem is that compinit was being called a whole bunch of extra times instead of just one time after the fpath was completely defined. I made those changes on my branch of oh-my-zsh on github. The changes have been discussed on github and they seem to be working well for a few people. Hopefully the changes will be merged into oh-my-zsh in the near future.

Pat Regan

Posted 2011-01-24T02:30:09.007

Reputation: 454

4For future readers, I believe all these changes have now been merged upstream. – Michael Mior – 2012-05-12T23:41:14.377

1Changes may have been moved upstream, but removing Git from plugins has helped me substantially. – kylehotchkiss – 2012-06-15T12:57:47.733

25

While ZSH has it's own fair-share of slowdowns, if you find the terminal window blank for a few moments before you see the Last Login: line, you are going to need to clear your log files to see speed improvements. This is still an issue as of OSX Lion and will need to be done every several months. Lame, I know.

The command is:

sudo rm -rf /private/var/log/asl/*.asl

Of course, you need to read this article beforehand and so you know exactly what is going on, because running anything that says sudo rm needs to be thought about. I only put this here because your use of ZSH proves your competence with the command line to start.

kylehotchkiss

Posted 2011-01-24T02:30:09.007

Reputation: 630

1Or even more foolproof, cd /private/var/log/asl then rm -f *.asl. Also for the record, this answer saved me from a startup time that was approaching 10 seconds, thanks a lot! – Garrett Disco – 2016-05-12T22:19:09.117

1Worked for me! BTW trash /private/var/log/asl*.asl. trash will need to be installed, of course. brew install trash – Mike D – 2017-01-16T13:26:02.473

1Thanks, such a simple solution for something that's been plaguing me for so long! – Dean – 2013-06-27T15:35:11.557

7I'd recommend not using -r since no directories are involved and it sn't needed. Omitting it leads to less tears if someone types sudo rm -rf / private/var/log/asl*.asl (space before private) by mistake. – Dean – 2013-06-27T15:39:25.327

12

My biggest improvement has come from removing items from the plugin=() section. The 'github' and 'brew' plugins are very slow to load.

I also removed hub which I had aliased togitand that sped up the prompt as well.

I've been using '/usr/bin/time zsh -i -c exit' to record the startup times, however compinit doesn't appear to make a big enough difference for me.

It'd be great to hear what others are doing to speed it up.

xer0x

Posted 2011-01-24T02:30:09.007

Reputation: 281

1Removing the github and brew plugins solved my problem immediately. – Peter Niederwieser – 2011-11-15T03:02:49.613

git and brew plugins are quite essential to me. By removing ruby plugin solved my problem. Thanks. – Ivan Z. G. Xiao – 2012-03-19T17:30:50.913

7

Are you using the pre-installed /bin/zsh or another one? I ask, because the zsh I have installed through fink starts terribly slow due its inclusion of zsh templates, while the vanilla starts right up.

Does running with an explicit dumpfile (compinit -d dumpfile) make it go faster? The man page states that

The next invocation of compinit will read the dumped file instead of performing a full initialization.

Joey1978

Posted 2011-01-24T02:30:09.007

Reputation: 91

2I am using Oh My Zsh (which I believe uses the default /bin/zsh). When I disable loading all of Oh My Zsh's plugins and whatnot, it loads up really quickly, but I think in that case compinit is never called. When I manually call compinit it takes a little while. Maybe it's just because Oh My Zsh adds so many bindings to compinit? – Eli – 2011-01-24T17:16:59.540

5

Now oh-my-zsh checks special git configuration option oh-my-zsh.hide-status before querying status. So run

git config oh-my-zsh.hide-status 1

on problematic repository.

Artem Tikhomirov

Posted 2011-01-24T02:30:09.007

Reputation: 487

for me I had to disable the git plugin in .zshrc. I am currently facing issues with slow internet response, which made zsh slow – Paschalis – 2016-08-12T02:02:28.600

5

Zsh on its own starts up in around 0.1 second for me, which is plenty good enough. I just noticed as I got near the 50,000 command history mark that it became more like 3 seconds to load up the first prompt.

I dunno how you guys are finding all these other reasons for slow startup, but mine was exactly what my first guess was. I did a mv ~/.zsh_history zsh_history_backup and bam, 3 second startup is now 0.1 second startup. Curiously, /usr/bin/time /bin/zsh -i -c exit fails to capture the time it takes to load in the history.

If you don't have tens of thousands of commands in your zsh history, though, then this isn't it. My ~/.zsh_history measured 1.8MB. Its very possible to accidentally paste a large chunk of stuff as a command into a terminal, this will also bloat history up right quick (though this is something very much to avoid doing as it can obviously be extremely destructive).

Steven Lu

Posted 2011-01-24T02:30:09.007

Reputation: 2 818

This is the answer. If haven't done anything fancy (a lot of plugins and addons) with oh-my-zsh, then I guarantee it's because of the history log. The change is immediate. – Sebastialonso – 2016-11-16T17:43:07.757

2

I was using the theme "af-magic"

Switching to "muse" solved the issue.

Edit ~/.zshrc and modify that line:

#ZSH_THEME="random"
#ZSH_THEME="af-magic"
ZSH_THEME="muse"

mika

Posted 2011-01-24T02:30:09.007

Reputation: 121

thanks for the answer, I found changing from af-magic to something else has solved for the problem. Interestingly restoring back af-magic still works great. not sure what went on under the hood. – sarat – 2016-01-22T05:11:56.160

0

I have really great speed improvements after clearing ~/.oh-my-zsh/plugins/ directory. There was many unused plugins inside it.

pinguinjkeke

Posted 2011-01-24T02:30:09.007

Reputation: 101

0

Add the following to your ~/.zshenv

skip_global_compinit=1

FacePalm

Posted 2011-01-24T02:30:09.007

Reputation: 101