Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is my current prompt:

user@machine ~/Projects/git/repo (git)-[name_of_the_branch] %

I want it to show me that there are Stash revisions by appending a 'S', like this:

user@machine ~/Projects/git/repo (git)-[name_of_the_branch][S] %

And the 'S' should be green :)

An even better way would be to append the number of stashed revision.

user@machine ~/Projects/git/repo (git)-[name_of_the_branch][Number_of_stashed_revisions] %

What do I need to change in .zshrc?

This is my current .zshrc file

share|improve this question
Wow! A 4348-line rc file! – Dennis Williamson Jan 29 '11 at 16:24
@Dennis: Nice eh? there is more lines in .zshrc_local:) Feel free to use it. It is awesome. – Nerian Jan 29 '11 at 16:25
up vote 1 down vote accepted

You first need to define a function that will test:

  • If you are in a git repo at all
  • If there are stashed changes

Luckily, the two can actually be combined into one. The command you are looking for is:

[ "$(git stash list 2>/dev/null)" != "" ]

If there is no repo, then the git stash list command itself will print an error to standard error, but nothing to standard out (it will incidentally return an error code in this case). If there is a repo, it will print nothing but not error. So testing against a blank string covers both of these.

Then you need to put this into your prompt. I am assuming that you are familiar with the $PS1 variable, so I will just show you a definition which shows only this:

PS1='$(if [ "$(git stash list 2>/dev/null)" != "" ]; then echo "[ S ]"; fi)'

The final part is adding color. You should be aware that color is added to your terminal by writing out escape codes. These codes are not rendered, but all text following them is altered. This means you not only need to turn them on, you also need to turn them off. Finally, since you are doing this in a prompt, you need to indicate the fact that they take no space (otherwise your terminal will redraw itself badly when you scroll through history).

Defining colors is usually done using this boilerplate:

# Try to load colors
autoload colors zsh/terminfo
if [[ "$terminfo[colors]" -ge 8 ]]; then
    eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
    eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'

Stick that into your .zshrc before you define $PS1 and you can refer to the colors using variables named $PR_COLOR-NAME. To make these take no space you need to wrap them in %{ VAR %}. This means you end up with something like:

PS1='$(if [ "$(git stash list 2>/dev/null)" != "" ]; then echo "%{$PR_GREEN%}[ S ]%{$PR_NO_COLOR%}"; fi)'

You can use $PR_LIGHT_GREEN if you prefer to not have the text bolded. Look up the available values in $terminfo for more text effects.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .