Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break line in terminal PS1 fix

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:

like image 678
Karolis Avatar asked Jan 08 '13 17:01

Karolis


1 Answers

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\]'
like image 63
Gordon Davisson Avatar answered Oct 19 '22 05:10

Gordon Davisson