Strange behavior in Terminal with custom .bash_profile
Solution 1:
You should surround your non-printing characters (in this case, your ANSI escape sequences) with bash escape sequences: \[
and \]
. That way bash will know the enclosed characters are not visible, ie. they do not take space, ie. they should not be included in the word-length calculation used by the line-wrapper.
in other words change your PS1
definition:
export PS1="\e[31m\u\e[0m@\e[34m\h\e[33m\w\n \$ \e[0m "
… to:
export PS1="\[\e[31m\]\u\[\e[0m\]@\[\e[34m\]\h\[\e[33m\]\w\n \$ \[\e[0m\] "
And here's the same PS1
value, for human–reading only, with perhaps improved readability (ANSI-escapes in the lower row, bash escapes in the middle and printing characters in the upper row.)
\u @ \h \w\n \$
\[ \] \[ \] \[ \] \[ \] \[ \]
\e[31m \e[0m \e[34m \e[33m \e[0m
Further reading:
- Bash Prompt HOWTO § 6.1. Colours
- Funtoo: Prompt Magic
- SuperUser: Do I need to enclose ANSI escape sequences in \[ and \] in a bash prompt?