A fast zsh prompt that packs in a lot of Git status info
Git Prompt Kit: Configurable, Fast Git Status Components For Custom Zsh Themes
Last Updated
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
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
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
Docs links for the curious:
- Parameters Used By The Shell (
psvar
) git-branch --show-current
- Redirection (
2>/dev/null
) - Complex Commands (
if…then…else
) - Conditional Expressions (
[[ -n … ]]
) - Array Parameters (
arr+=( el )
) git-rev-parse --short
git-status --porcelain
- Manipulating Hook Functions (
add-zsh-hook
) - Hook Functions (
precmd
) - Conditional Substrings in Prompts (
%v
,%(…)
) - Prompt Expansion > Visual effects (
%F
,%f
) - Prompt Expansion > Shell state (
%~
,%#
)
- Parameters Used By The Shell (
As measured by zsh-prompt-benchmark ↩︎
See https://github.com/romkatv/zsh-bench#how-fast-is-fast ↩︎
Git Prompt Kit uses gitstatus, an accelerated alternative to Git’s own status commands. ↩︎
Articles You Might Enjoy
-
Hometown: A Dynamic, Highly Configurable Git-Focused Zsh Theme
-
Watch for specific added nodes with MutationObserver
MutationObserver makes it easy to watch for the addition of specific nodes, if you know where to drill.
-
Add high-performance search to static sites with PageFind
The concise guide to using Pagefind for your static site’s search functionality.