views:

171

answers:

2

Here's my PS1 variable:

PS1='\u:\W$(__git_ps1 "\e[32m\][%s]\e[0m\]")$ '

Works great for picking up my Git branch, but it has the unfortunate side-effect of wrapping the lines when the colours are active, so that they overlap when you use long commands.

Can anyone with magic PS1 skills help me out to fix this?

+2  A: 

Got it, needed to escape the colours properly.

Fix:

PS1='\u:\W$(__git_ps1 "\[\e[32m\][%s]\[\e[0m\]")$ '
Aupajo
+2  A: 

May I suggest the following method for colors in Bash, it makes the code much more readable and alot harder for you to miss an escape or two.

Put the following in your ~/.bashrc

BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
LIME_YELLOW=$(tput setaf 190)
YELLOW=$(tput setaf 3)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)

A sample PS1 (or really anything that prints to the screen) would be:

 PS1="\[${WHITE}\](\[${YELLOW}\]\u@\h\[${WHITE}\])\[${NORMAL}\]$ "

You need only put \[ \] around the color words.

If you have a 256-color terminal, you can experiment with other numerical values to 'tput setaf' all the way up to 255.

SiegeX
Oh that is simple, clear, and awesome. Have some rep, on the house.
Aupajo