Linux: coloring bash prompt will break carriage return
Solution 1:
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\]"
Solution 2:
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.