Oh-My-Zsh Performance – How to Fix a Slow Oh-My-Zsh Prompt

oh-my-zshpromptterminalzsh

I'm using macOS 10.15.2 with iTerm2, zsh 5.7.1 and oh-my-zsh (theme robbyrussell).

I noticed that the prompt print is slightly slow respect to the bash one. For example, if I press enter, cursor initially goes at the beginning of the next line then, after a little while, the shell prompt comes in and the cursor is moved to its natural position. For example, if → ~ is the prompt when I'm in my home folder, and [] is my cursor, when I press enter I see:

0 – Idle status

→ ~ []

1 – Immediately after pressing enter

[]

2 – Back to idle status

→ ~ []

This slowness is particularly evident when I quickly press enter multiple times. In this case, I see some blank lines. This is what I see

→ ~
→ ~
→ ~

→ ~

→ ~


→ ~
→ ~
→ ~

→ ~ []

I come from bash shell and when I use bash, there is not such a slowness. I'm not sure this is an issue of oh-my-zsh or its natural behavior. I'd like to know more about this and, eventually, how to fix it. Thanks.

PS: the problem comes from oh-my-zsh and it persists even if I disable all the plugins.

PPS: I previously posted this question on SO. Thanks to user1934428 for his help and for suggesting me to move this question here.

Best Answer

I don't know what oh-my-zsh puts in the prompt by default. Maybe it tries to identify the version control status, that's a very popular prompt component which might be time-consuming.

To see what's going on, turn on command traces with set -x.

→ ~ 
→ ~ set -x
trace of the commands that are executed to calculate the prompt
→ ~ 
trace of the commands that are executed to calculate the prompt
→ ~ set +x
+zsh:3> set +x
→ ~ 
→ ~ 

If the trace is so long that it scrolls off the screen, redirect it to a file with

exec 2>zsh.err

This directs all error messages to the file, not just the trace. To get traces and errors back on the terminal, run

exec 2>/dev/tty

You can customize the trace format through PS4. This is a format string which can contain prompt escapes. For example, to add precise timing information:

PS4='%D{%s.%9.}+%N:%i> '
Related Question