21

I wanted to use one of the Git-completion.bash features but I can't customize the look I'd like to have. Here is relevant part of my .bash_profile:

source ~/.git-completion.bash

function prompt
{
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[0;34m\]"
export PS1="
${GREEN}\u${CYAN}@${BLUE}\h ${CYAN}\w $(__git_ps1 '(%s)') ${GRAY}
$ "
}
prompt

and it doesn't show the branch name.

However, if I replace export PS1 above with the one below, it works as expected:

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

I guess it's some apostrophe / quotation marks issue.

How should I correct the 1st version to get it to work?

Piotr Byzia
  • 3,363
  • 7
  • 42
  • 62

2 Answers2

33

The trick to get the quoting right is to have eveything double-quoted, except for $(__git_ps1 "(%s)"), which is single-quoted.

source ~/.git-completion.bash
function prompt
{
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[0;34m\]"
export PS1="
${GREEN}\u${CYAN}@${BLUE}\h ${CYAN}\w"' $(__git_ps1 "(%s)") '"${GRAY}"
}
prompt

An alternative solution is to replace $( with \$( in the code in the question.

Background information: Two substitutions take place: first at export PS1="..." time, and later when the prompt is displayed. You want to execute __git_ps1 each time the prompt is displayed, so you have to make sure that the first substitution keeps $(...) intact. So you write either '$(...)' or "\$(...)". These are the two basic ideas behind the solutions I've proposed.

pts
  • 80,836
  • 20
  • 110
  • 183
  • 1
    Using the locals for the colors is a nice trick to make the thing readable. I'm stealing that idea for sure. FYI for others, I kind of like using yellow for my path - `local YELLOW="\[\033[0;33m\]"` – studgeek Mar 08 '12 at 19:31
5

Not sure, but vcprompt might solve it better for you?

Macke
  • 24,812
  • 7
  • 82
  • 118
  • 1
    It probably doesn't cover all the options of __git_ps1 in git-completion.bash, but I really like the fact that it gives information about other version control systems as well. Thanks for the tip! – Mark van Lent Sep 16 '09 at 17:42