11

I have this code to color my terminal:

export PS1="\e[1;30m\][\e[\e[1;30m\]\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\] \e[1;30m\]]\n[\[ \e[1;31m\]\T\[\e[0m\]\e[1;30m\] ] > \e[37m\]"

But I have one problem, when text should be in the new line it overwrites the first line.

Example:

Cœur
  • 32,421
  • 21
  • 173
  • 232
ThiSGUy
  • 309
  • 1
  • 4
  • 12

1 Answers1

16

In order for bash to figure out how much screen space your prompt takes up (and therefore where the actual command line starts), you have to enclose the non-printing parts of the prompt in \[...\]. Mostly, that means escape sequences like \e[1;30m need to be written as \[\e[1;30m\]. You have some \['s and \]'s in your prompt, but they're in the wrong places, which is making bash very confused. Finding all the printing and non-printing parts of a prompt as complex as yours is not trivial, but I think this gets it right:

export PS1='\[\e[1;30m[\e[\e[1;30m\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\] \[\e[1;30m\]]\n[ \[\e[1;31m\]\T\[\e[0m\e[1;30m\] ] > \[\e[37m\]'
Gordon Davisson
  • 95,980
  • 14
  • 99
  • 125
  • 1
    I had the same problem, thanks for pointing out that you need to have ever non-printing part enclouded in `\[` ... `\]` – pbojinov Jul 29 '13 at 23:25
  • 1
    Brilliant - without surrounding the color escape sequences in PS1 with `\[` and `\]` as you state, the cursor position gets out of sync when using up-arrow to select the previous command in the shell history. – Brent Faust Jan 13 '15 at 22:45