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).


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\]"
  • 4
    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. Feb 27 '13 at 15:27
  • 1
    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
  • 2
    @osirisgothra: The documentation is actually talking about a different thing – skip-csi-sequence is meant to suppress input sequences generated by special keys.
    – user1686
    Aug 22 '14 at 10:34
  • 1
    @MaxHowell you have to wrap each \e[xxm sequence, not the whole string.
    – Enrico
    May 5 '16 at 21:12
  • Thanks. In my case I just replaced \e[39m with \[\e[;39m\] and it works fine
    – Herrgott
    Jun 3 '18 at 8:03

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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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