jealous of Ondrej's (branch) bash prompt

6 views
Skip to first unread message

Ryan Krauss

unread,
Jun 23, 2009, 10:10:17 AM6/23/09
to sy...@googlegroups.com
After watching Ondrej's git videos, I became jealous of the (branch name) at the end of his bash prompt.  I googled around and was able to put something together that I like.  The trick was getting it to work in color.  Especially tricky for me was getting back to the default white after the branch name.  I found that I could get either light gray or bold white, neither of which I liked.  But a code that doesn't really make sense to bash seems to get me back to the default.

I added these to my .bashrc:
function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="\u@\h:\w\[\e[0;33m\]\$(parse_git_branch)\[\e[0;39m\]$ "

where \[\e[0;33m\] switches to a medium yellow and \[\e[0;39m\] switches back to the default white (for me).

I borrowed ideas from these places:

How to get the branch name in the prompt:
http://eddorre.com/tags/bash

how to tweak the other things in the prompt:
http://www.linuxselfhelp.com/howtos/Bash-Prompt/Bash-Prompt-HOWTO-2.html

and how to put things in color:
http://wiki.archlinux.org/index.php/Color_Bash_Prompt

Ondrej may have a cleaner solution, but I like it a lot.

Ryan

Ondrej Certik

unread,
Jun 23, 2009, 1:21:37 PM6/23/09
to sy...@googlegroups.com
Hi Ryan,

Sorry that I forgot to say how to do it. The way you did is one way,
but it's too complex. I wrote how to do it here in a comment:

http://asmeurersympy.wordpress.com/2009/06/22/how-to-permanently-lose-data-with-git-and-then-retrieve-it-again/

Basically Aaron's post is that he lost some data, because he didn't
know he was doing a rebase. So I replied:


--------------
It’s because you don’t color your prompt with the name of the branch
(and the prompt also changes to something like “master|REBASE” if you
rebase). See here how it looks like:

http://code.google.com/p/sympy/wiki/GitTutorials

so I very, very strongly suggest you use that.

http://blog.ericgoodwin.com/2008/4/10/auto-completion-with-git

e.g. here is my PS1 prompt:

PS1=’${debian_chroot:+($debian_chroot)}\u@\h:\w\[33[31m\]$(__git_ps1
“(%s)”)\[33[00m\]\$ ‘

if you use Mac, source the “contrib/completion/git-completion.bash”
file (in the git repository for git), which contains the definition of
the __git_ps1 and some documentation too (read it if you have troubles
getting it work).

Once you have the colored prompt, I am pretty sure it will never happen again.
-----------------


Ondrej

Ryan Krauss

unread,
Jun 23, 2009, 2:58:05 PM6/23/09
to sy...@googlegroups.com
Cool.  Thanks for that.  I like the idea of using a built in completion function.  I couldn't quite make the colors work with your PS1.  Here is what I ended up with:
PS1='\u@\h:\w\[\e[0;33m\]$(__git_ps1 " (%s)")\[\e[0;39m\]$ '

Ryan

Aaron S. Meurer

unread,
Jun 23, 2009, 3:32:10 PM6/23/09
to sy...@googlegroups.com
I came up with this: export PS1="\[\e[31;40m\]\h:\W \u\$\[\e[0m\]\
[\e[0;33m\]$(__git_ps1 "(%s)")\[\e[31;40m\]$\[\e[0m\]" . It works,
only I get

Macintosh:~ aaronmeurer$$

when I am not in a git directory. Do you know if it possible to get
rid of the extra $ when I am not in a git directory. Also, is there
any way to get a different color if I am in a rebase (does PS1 have an
if-else construct, or does can it auto update from something?)?

Thanks for the link too. Autocompletetion in git is awesome.

By the way, I think the \[\e[0m\] in my PS1 puts it back to the
default color.

Aaron Meurer

asmeurer

unread,
Jun 23, 2009, 4:01:56 PM6/23/09
to sympy
OK I fixed that. I had too many $'s. The problem now is that it
doesn't change when I switch a branch or after cd'ing into and out of
a git directory. Ondrej's PS1 changes (though I get
aaronmeurer@Macintosh:~/Documents/python/sympy/sympy33[31m(odes|
BISECTING)33[00m$ for my prompt on Mac OS X), but I cannot figure out
what is different in it. This is kind of useless unless it changes.

Here is what I have: export PS1="\[\e[31;40m\]\h:\W \u\[\e[0m\]\[\e
[0;33m\]$(__git_ps1 "(%s)")\[\e[31;40m\]\$\[\e[0m\]"

Aaron Meurer
On Jun 23, 1:32 pm, "Aaron S. Meurer" <asmeu...@gmail.com> wrote:
> I came up with this: export PS1="\[\e[31;40m\]\h:\W \u\$\[\e[0m\]\
> [\e[0;33m\]$(__git_ps1 "(%s)")\[\e[31;40m\]$\[\e[0m\]" .  It works,  
> only I get
>
> Macintosh:~ aaronmeurer$$
>
> when I am not in a git directory.  Do you know if it possible to get  
> rid of the extra $ when I am not in a git directory.  Also, is there  
> any way to get a different color if I am in a rebase (does PS1 have an  
> if-else construct, or does can it auto update from something?)?
>
> Thanks for the link too.  Autocompletetion in git is awesome.
>
> By the way, I think the \[\e[0m\] in my PS1 puts it back to the  
> default color.
>
> Aaron Meurer
> On Jun 23, 2009, at 11:21 AM, Ondrej Certik wrote:
>
>
>
>
>
> > Hi Ryan,
>
> > On Tue, Jun 23, 2009 at 8:10 AM, Ryan Krauss<ryanli...@gmail.com>  
> >http://asmeurersympy.wordpress.com/2009/06/22/how-to-permanently-lose...

Ryan Krauss

unread,
Jun 23, 2009, 4:33:39 PM6/23/09
to sy...@googlegroups.com
I don't see anything obivously wrong with yours unless the linefeed is not just in your email.  Does this one work without color?:

PS1='\u@\h:\w$(__git_ps1 " (%s)")$ '

if that works, try putting in things like
\[\e[0;33m\]
to switch to a new color.

If that one doesn't work correctly without color, then I would guess that something is wrong with __git_ps1.

But I am not an expert at this.

Ryan

Ryan Krauss

unread,
Jun 23, 2009, 4:35:00 PM6/23/09
to sy...@googlegroups.com
(I accidentally managed to copy a line feed and some non-ascii single and double quotes by mistake and it took me a little bit to realize why it wasn't working.)

Ondrej Certik

unread,
Jun 23, 2009, 4:41:05 PM6/23/09
to sy...@googlegroups.com
On Tue, Jun 23, 2009 at 2:01 PM, asmeurer<asme...@gmail.com> wrote:
>
> OK I fixed that.  I had too many $'s.  The problem now is that it
> doesn't change when I switch a branch or after cd'ing into and out of
> a git directory.  Ondrej's PS1 changes (though I get
> aaronmeurer@Macintosh:~/Documents/python/sympy/sympy33[31m(odes|
> BISECTING)33[00m$ for my prompt on Mac OS X), but I cannot figure out
> what is different in it.  This is kind of useless unless it changes.
>
> Here is what I have: export PS1="\[\e[31;40m\]\h:\W \u\[\e[0m\]\[\e
> [0;33m\]$(__git_ps1 "(%s)")\[\e[31;40m\]\$\[\e[0m\]"

Ok, start with thing that works, e.g. my prompt, and remove some of
the color changing sequences that don't seem to work in your terminal.

Post here when you figure it out, we'll put it into the documentation,
I plan to overhaul this page soon:

http://docs.sympy.org/sympy-patches-tutorial.html

with videos and updodate info about git. Any help is of course
welcome. :) You can help for example by posting here a prompt that
works etc.

Ondrej

Aaron S. Meurer

unread,
Jun 24, 2009, 12:33:40 AM6/24/09
to sy...@googlegroups.com
OK, so this is really weird. It turns out that the problem was that I
was using double quotes. When I changed it to single quotes as in

PS1='\[\e[31;40m\]\h:\W \u\[\e[0;33m\]$(__git_ps1 "(%s)")\[\e[31;40m\]$
\[\e[0m\]'

It changes as expected. Is the behavior the same in Linux? If so, it
probably has something to do with the way bash handles single quote
and double quote strings that I don't know about because I don't know
any bash. Otherwise, it is a strange Mac OS X "feature".

A commenter on the blog you linked to said that you need to escape the
first $ to get this to work, but for me escaping makes it not work.
You might put that in there too though because it might be different
for linux or something.

I wouldn't use my prompt as an example, as it looks kind of bad if
your terminal does not have a black background and all the color stuff
kind of distracts from the point anyway. Probably a better example is

export PS1='\w$(__git_ps1 "(%s)")$'

which is pretty simple and straight forward (and works on my machine).

Lastly, reading the /git-completion.bash file, it looks like you can
set the GIT_PS1_SHOWDIRTYSTATE environment variable to a non-empty
value to show a * next to the branch name for unstaged changes and a +
for staged changes and the GIT_PS1_SHOWSTASHSTATE environment variable
to a nonempty value to show a $ next to the branch name if something
is stashed.

Aaron Meurer

Ryan Krauss

unread,
Jun 24, 2009, 9:14:47 AM6/24/09
to sy...@googlegroups.com
I think the single quote vs. double quote thing is because there are double quotes inside of the string.  If I change the outside single quotes to double, I get strange behavior like you mentioned earlier.

Robert Kern

unread,
Jun 24, 2009, 4:41:22 PM6/24/09
to sy...@googlegroups.com
On Wed, Jun 24, 2009 at 08:14, Ryan Krauss<ryan...@gmail.com> wrote:
> I think the single quote vs. double quote thing is because there are double
> quotes inside of the string.  If I change the outside single quotes to
> double, I get strange behavior like you mentioned earlier.

In addition, bash does not expand $variables inside '' but does expand
them inside "". That may be why one source suggested that the $ needed
to be escaped and what Aaron saw.

http://www.gnu.org/software/bash/manual/bashref.html#Single-Quotes

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco

Aaron S. Meurer

unread,
Jun 25, 2009, 12:23:32 AM6/25/09
to sy...@googlegroups.com
Another thing that I have found is that I need to have the "source
~/.git-completion.bash" line at the very bottom of my profile for git
auto-completion to work properly. Otherwise, it only auto-completes
aliases from my .gitconfig file.

Aaron Meurer
On Jun 23, 2009, at 2:41 PM, Ondrej Certik wrote:

>
Reply all
Reply to author
Forward
0 new messages