Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

https://gist.github.com/801944

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
add comment

1 Answer 1

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
    colors
fi
for color in RED GREEN YELLOW BLUE MAGENTA CYAN WHITE; do
    eval PR_$color='%{$terminfo[bold]$fg[${(L)color}]%}'
    eval PR_LIGHT_$color='%{$fg[${(L)color}]%}'
done
PR_NO_COLOR="%{$terminfo[sgr0]%}"

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.