Strange behavior in Terminal with custom .bash_profile

bashterminal

It's hard to describe the behavior, but here's a short clip:

http://www.youtube.com/watch?v=9KqHBA94FPI

Basically, when entering multi-line commands, at the end of the first line, the insertion block goes back to the start of the first line, writes over the first line, then continues like normal on to the second, third, etc. lines. When deleting, it allows me to delete the whole command, plus the blank spaces on the line above the command (I have it setup so commands get inserted after a line containing user@host/Directory information. It then even allows me to delete the last part of the Directory and host I described above. Sorry I'm terrible at explaining this, the video will do a better job.

Here is the contents of my .bash_profile:

export PS1="\e[31m\u\e[0m@\e[34m\h\e[33m\w\n \$ \e[0m "

Best Answer

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: