70

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?

10 Answers 10

35

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.

| improve this answer | |
  • 4
    For future readers, I believe all these changes have now been merged upstream. – Michael Mior May 12 '12 at 23:41
  • 1
    Changes may have been moved upstream, but removing Git from plugins has helped me substantially. – kylehotchkiss Jun 15 '12 at 12:57
30

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.

| improve this answer | |
  • 1
    Thanks, such a simple solution for something that's been plaguing me for so long! – Dean Jun 27 '13 at 15:35
  • 7
    I'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 Jun 27 '13 at 15:39
  • 1
    Or 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 May 12 '16 at 22:19
  • 1
    Worked for me! BTW trash /private/var/log/asl*.asl. trash will need to be installed, of course. brew install trash – Mike D Jan 16 '17 at 13:26
13

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.

| improve this answer | |
  • 1
    Removing the github and brew plugins solved my problem immediately. – Peter Niederwieser Nov 15 '11 at 3:02
  • git and brew plugins are quite essential to me. By removing ruby plugin solved my problem. Thanks. – Ivan Z. G. Xiao Mar 19 '12 at 17:30
8

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.

| improve this answer | |
  • 2
    I 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 Jan 24 '11 at 17:16
6

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.

| improve this answer | |
  • 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 Aug 12 '16 at 2:02
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).

| improve this answer | |
  • 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 Nov 16 '16 at 17:43
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"
| improve this answer | |
  • 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 Jan 22 '16 at 5:11
1

These are the steps which I have used to optimize my shell startup speed and reducing lag in executing commands -

  1. If you are using powerlevel9k, then I will recommend to immediately switch to powerlevel10k.

Powerlevel10k is a theme for Zsh. It emphasizes speed, flexibility and out-of-the-box experience. It is a reimplementation of the popular Powerlevel9k zsh theme. It looks exactly the same given the same configuration but renders prompt 10-100 times faster. It's optimized on every level of the stack, all the way down to using a patched version of libgit2 that can scan a repo 4 times faster than the original. It can remove Zsh startup lag even if it's not caused by a theme with features such as Instant Prompt.

  1. Go to Preferences -> Profiles -> General -> Command and select the option Command instead of Login Shell and paste the below command in the box nearby it.
login -pfq username /usr/local/bin/zsh -il

You wouldn't see the last login time printed when starting a new tab now. If zsh is not present in the location /usr/local/bin/zsh, you will need to install zsh using brew. The default zsh provided by mac is at /usr/bin/zsh and might be using an older version like 5.2 which can cause slow speed when used with iTerm or oh-my-zsh.

  1. To make pasting in zsh fast, execute the below command in the terminal.
mkdir -p $ZSH_CUSTOM/lib && touch $ZSH_CUSTOM/lib/misc.zsh
  1. Point 2 should already take care of slow login times. But just for safety execute the below command
mkdir -p .hushlogin
  1. There are tons of plugins you might be using which are slow and creates lag. You need to point out these plugins and remove them. For this, you will need zsh profiling. Follow this link for more details -

https://stevenvanbael.com/profiling-zsh-startup

| improve this answer | |
  • Welcome to Super User! Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill Mar 28 at 7:13
0

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

| improve this answer | |
0

Add the following to your ~/.zshenv

skip_global_compinit=1
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.