Information-richness in a compact zsh Prompt

Note: While this post focuses on zsh, everything I do should be possible to implement in bash too.

When I look at people’s shell prompts, the first thing I always seem to notice is that they’re bulky and eye-grabbing. Seeing a textual version of GNOME Panel get duplicated into my terminal after every command isn’t something that appeals to me, so I thought I’d come up with something just as useful, but a little more compact.

What follows is a step-by-step story, with pseudo-screenshots, of how I went from a bog-standard zsh prompt to a fairly advanced prompt that only looks different when it has something to say, ending in a link to the .zshrc source I use to actually implement it. Feel free to skip ahead.

Because I like the color scheme and because I already have it, I’m starting with the default Gentoo prompt:

ssokolow@monolith ~ %

Obviously, this already has the usual Gentoo prompt’s “you are root” indicators (user ID turns red and % turns into #) and the ability to tell zsh and bash apart at a glance (For those who don’t know, zsh uses % while bash uses $) but I do a lot of programming and I use git for all my code, so the most useful thing I could possibly add is a way to never forget which branch I’m working on. Thankfully, that’s already been done for me:

ssokolow@monolith quicktile [master] %

I’d also wanted a way to see, at a glance, whether there were any jobs running in the background. The %(1j.%%.) ternary expression fixed that one right up:

ssokolow@monolith quicktile [master] % git gui &
ssokolow@monolith quicktile [master] %%

Finally, not all of my programs are clear about when they send a non-zero exit code, so it’d be nice to know that too. Another ternary expression, namely %(0?..%F{yellow}), takes care of that:

ssokolow@monolith quicktile [master] % false
ssokolow@monolith quicktile [master] %

I like how the prompt, by default, only displays the last component of $PWD so I left that unchanged. You’re free to add in other things but I tend to struggle with performance issues when both Firefox and GCC have to fight for “only” 4GiB of RAM so I decided not to hook in any more subprocess calls.

A pruned-down, streamlined version of the code for it is available on GitHub as zshrc.d/gentoo_prompt_setup but you’ll also need this line from my master .zshrc:

typeset -ga precmd_functions

CC BY-SA 4.0 Information-richness in a compact zsh Prompt by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This entry was posted in Geek Stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution under the same terms as the associated post.

All comments are moderated. If your comment is generic enough to apply to any post, it will be assumed to be spam. Borderline comments will have their URL field erased before being approved.