Customization Tips to make Bash/Zsh prompts faster and more comfortable

The terminal prompt is a UI that you see hundreds of times every day.
However, some customizations tend to cause problems such as “slow,” “too much information and difficult to read,” and “slow completion.

In this article, we will introduce specific customization tips to make Bash/Zsh prompts faster, more comfortable, and easier to read.
The focus is on Zsh, but the tips can be applied to Bash as well.

1. Find out what makes a prompt slow

The main causes of slow prompts are

  • Getting Git status information each time
  • Heavy theming, such as Powerlevel10k
  • Many hooks being executed (precmd / PROMPT_COMMAND )
  • Calling external commands when drawing prompts
  • Large loading cost for plugins

Running external commands every time they are displayed can slow things down by several hundred ms to nearly a second.

2. Reduce the load on Git information

Git status is a very heavy process.
Here are a few ways to speed up the process. 2-1.

2-1. use gitstatus to speed up the process (Zsh)

You can use gitstatusd used in Powerlevel10k by itself.
It allows fast status retrieval even on Git folders and solves most of the prompt delays.

brew install romkatv/gitstatus/gitstatus

You can speed up the process by simply calling it with a Zsh hook.

2-2. minimize Git information

It’s faster if you only show the branch name and not the number of changes or diffs.

parse_git_branch() {
  git branch 2>/dev/null | sed -n '/\* /s///p'
}
PROMPT='%n@%m:%~ $(parse_git_branch)%# '

The same is available in Bash. 2-3.

2-3. hide Git information in large repositories

In large repositories with tens of thousands of files, just showing Git information slows down the process.

if [[ $(git rev-parse --show-toplevel 2>/dev/null | wc -c) -gt 0 && $(find . -maxdepth 1 | wc -l) -lt 500 ]]; then
  PROMPT='%F{green}($(parse_git_branch))%f %~ %# '
else
  PROMPT='%~ %# '
fi

3. Make the prompt structure minimal

The more information in a prompt, the better.
In practice, the following is the optimal amount of frequently used information

  • Your current directory
  • Your Git branch
  • Python/Node or other version (optional)
  • Exit code (optional)

Minimal information is also faster.

Example: Minimal Zsh prompt

PROMPT='%n@%m %~$(parse_git_branch) %# '

4. Changing the plugin manager to a fast one

For Zsh

  • Slow: oh-my-zsh (useful but heavy)
  • Fast: oh-my-zsh (useful but heavy)
    • Antidote
    • Znap
    • Zinit (requires fast configuration)

Antidote alone is fast enough.

git clone --depth=1 https://github.com/mattmc3/antidote.git ~/.antidote
echo "source ~/.antidote/antidote.zsh" >> ~/.zshrc
echo "antidote load" >> ~> ~/.zshrc

5. Do not write external commands at the prompt

Avoid calling external commands such as date or whoami at the prompt.
Instead, express them in the shell built-in.

Slow example

PROMPT='$(date "%H:%M:%S") %~ %# '

Fast example (Zsh built-in)

PROMPT='${strftime("%H:%M:%S")} %~ %# '

6. Appropriate use of completion tools such as fzf / zoxide

Optimizes operation speed, not prompt speed, but greatly improves the development experience.

fzf

brew install fzf
$(brew --prefix)/opt/fzf/install

zoxide

brew install zoxide
echo 'eval "$(zoxide init zsh)"' >> ~/.zshrc

This is a convenient tool that allows you to instantly go to past directories with z.

7. Points to speed up in Bash as well

Bash has fewer prompts than Zsh, but it can be faster in the following ways

Remove unnecessary settings in .bashrc

  • brew completion
  • nvm in older versions
  • git-prompt
  • Multiple loading of pyenv configurations

etc. can cause delays.

Shorten PROMPT_COMMAND

Writing multiple commands slows down each time a prompt appears.

8. Example of a fast and comfortable prompt (Zsh)

Finally, here is an example of a fast version prompt that I use.

# git branch (fast version)
parse_git_branch() {
  git rev-parse --abbrev-ref HEAD 2>/dev/null
}

# prompt definition
PROMPT='%F{cyan}%n%f %F{yellow}%~%f %F{green}$(parse_git_branch)%f %# '

The external command is git only, and the displayed content is minimal, so it is very fast.

Summary

The Bash/Zsh prompt can be made much more comfortable by customization.
In particular, the following points can speed things up

  1. Optimize handling of Git information
  2. Keep the prompt structure concise
  3. Don’t execute external commands inside the prompt
  4. Make the plugin manager faster
  5. Make navigating and completion more comfortable with fzf and zoxide
Copied title and URL