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.

When I try to colorize the bash prompt like this:

export PS1="\e[0;31m[\u@\h \W]\$ \e[m "

it breaks the carriage return (instead of going to a new line when line is full, it starts in the same line and erases the input text).

If I remove the color sequence it works fine.
I use Ubuntu 11.10 and terminal (guake has the same bug too).

share|improve this question
    
Strange. It works w/o any problem on RedHat 5 and Mac OS X –  Matteo Dec 12 '11 at 20:08
    
possible duplicate of PS1 problem, cmd is looping –  grawity Dec 12 '11 at 20:10

2 Answers 2

up vote 16 down vote accepted

You must wrap all escape sequences between \[ and \] in order for 'readline' to correctly calculate the prompt's visible length. Without them, 'readline' thinks the prompt is longer than it is, and doesn't know when the text reaches the margin.

PS1="\[\e[;31m\][\u@\h \W]\$ \[\e[m\]"
share|improve this answer
    
Thx you, it what I expect. –  Abonec Dec 12 '11 at 20:33
1  
Thanks a ton!! i was trying to find some documentation on it in the official docs but couldnt -- the 'stock' color prompt does employ this 'trick' and I had a hard time at first figuring out what exactly it was for, so thanks for clearing that up --- btw i did actually find the documentation source in 'man readline 3' around line 684 and it says that this is actually called 'skip-csi-sequence' for those who want to know where the documentation on this is. I don't think i would have found it without you pointing me to readline(3) even though i knew about it before-failed to make the connection. –  osirisgothra Feb 27 '13 at 15:27
    
What does "wrap" mean? Do you mean wrap the whole thing IN a \[, \]? I guess so, but it doesn't seem to be working for me. –  Max Howell Nov 2 '13 at 20:33
    
@osirisgothra: The documentation is actually talking about a different thing – skip-csi-sequence is meant to suppress input sequences generated by special keys. –  grawity Aug 22 at 10:34

the [ and ] tip worked on bash on OS X 10.8.2 too.

I use:

PS1="[\e[0;37m\W\$(git branch 2>/dev/null | grep -e '* ' | sed 's/^..(.*)/{\1}/') \$ \e[m ]"

which shows me my current git branch in use.

share|improve this answer
    
Do you know about "__git_ps1" ? –  rafak May 5 '13 at 4:43
    
nopes. what magical thing will it do? –  stonefruit May 19 '13 at 19:06

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.