Git Prompt Kit: Configurable, Fast Git Status Components For Custom Zsh Themes

Last Updated

A collection of components for displaying Git information in your prompt
null

Running Git commands between prompts to add status information in your command line prompt can significantly increase the time it takes your zsh prompt to render. And if you aren’t already familiar with zsh prompt code idioms and less-common Git commands, it can take a lot of research, and trial and error.

With Git Prompt Kit, a collection of Git status info zsh prompt components, your prompt can stay snappy, and your prompt-customizing the code you write is clear and terse.

Try it out!

The clear and concise nature of Git Prompt Kit-powered zsh themes is easy to show in an article, but the components’ dynamic content and customizability are not. For that, install Git Prompt Kit… or try out the interactive web demo!

Say you want your prompt to show

  • the current working directory, colorized
  • if you’re in a Git repo, the checked out branch of (if HEAD is detached) commit, colors according to whether or not the working tree is dirty
  • % when you aren’t root and # when you are, colored according to whether or not the previous command errored.

One vanilla implementation is

~/.zshrc
shell
shell
set_prompt_vars() {
psvar=( )
git_branch=$(git branch --show-current 2>/dev/null)
if [[ -n $git_branch ]]; then
psvar+=( $git_branch )
else
psvar+=( $(git rev-parse --short 2>/dev/null) )
fi
if [[ $(git status --porcelain 2>/dev/null) ]]; then
psvar+=( magenta )
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd set_prompt_vars
# directory
PROMPT='%F{blue}%2~%f '
# Git HEAD
PROMPT+="%(2V.%F{%2v}.)%1v%f%(1V. .)"
# prompt char
PROMPT+='%F{%(?.green.red)}%#%f '
shell
set_prompt_vars() {
psvar=( )
git_branch=$(git branch --show-current 2>/dev/null)
if [[ -n $git_branch ]]; then
psvar+=( $git_branch )
else
psvar+=( $(git rev-parse --short 2>/dev/null) )
fi
if [[ $(git status --porcelain 2>/dev/null) ]]; then
psvar+=( magenta )
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd set_prompt_vars
# directory
PROMPT='%F{blue}%2~%f '
# Git HEAD
PROMPT+="%(2V.%F{%2v}.)%1v%f%(1V. .)"
# prompt char
PROMPT+='%F{%(?.green.red)}%#%f '

Coming up with that requires knowledge of zsh function autoloading, hooks, arrays, conditionals, output redirection and coloring, psvar, and prompt expansion; and knowledge of Git commands for checked-out branch’s name, the checked-out commit’s SHA, and whether the working tree is dirty.[1]

On my machine, this vanilla-code prompt takes about 80ms to render[2] which slow enough that you’re likely to perceive lag[3].

With Git Prompt Kit components, entire thing becomes

~/.zshrc
shell
shell
# load Git Prompt Kit and then
PROMPT='$GIT_PROMPT_KIT_CWD '
PROMPT+='${GIT_PROMPT_KIT_HEAD:+$GIT_PROMPT_KIT_HEAD }'
PROMPT+='$GIT_PROMPT_KIT_CHAR '
shell
# load Git Prompt Kit and then
PROMPT='$GIT_PROMPT_KIT_CWD '
PROMPT+='${GIT_PROMPT_KIT_HEAD:+$GIT_PROMPT_KIT_HEAD }'
PROMPT+='$GIT_PROMPT_KIT_CHAR '

On my machine, the Git Prompt Kit-based prompt takes under 15ms to render[4], which is fast enough that you’re very unlikely to perceive a delay.

It has the same features plus enhancements:

  • The current directory is underlined if it’s a Git repo root.
  • If you’re in a subdirectory in a Git repo, the root and the current directory are both shown

And that’s the power of just three of Git Prompt Kit’s nearly thirty components!

Another project to check out

Hometown is my zsh theme built on Git Prompt Components. Read about it in Hometown: A Dynamic, Highly Configurable Git-Focused Zsh Theme

Learn more in the Git Prompt Kit docs.

If you like it, or just feel like saying “nice work”, let me know by starring the GitHub repo.


Footnotes

  1. Docs links for the curious:

    ↩︎
  2. As measured by zsh-prompt-benchmark ↩︎

  3. See https://github.com/romkatv/zsh-bench#how-fast-is-fast ↩︎

  4. Git Prompt Kit uses gitstatus, an accelerated alternative to Git’s own status commands. ↩︎

Articles You Might Enjoy

Or Go To All Articles