XFCE terminal – change color of input prompt screws up the input line

xfce4-terminal

This question is a follow up on the answer to this question, regarding changing the color of the user input prompt in the XFCE terminal: Ubuntu terminal (Xfce): making input commands different font color from output text

Since I am not allowed to comment there (min 50 reputation), I had to open a new question.

Following the answer, I used the following command:

PS1='\e[1;36m\u@\h \W]\$\e[0m '

This keeps the format the same as default, but changes the color to cyan, as expected.

However, I am getting some very strange behavior. Sometimes the cursor will jump back on the input line, and sometimes it will show old text that should not be there. Sometimes it splits into two lines such that I continue typing on the line above. For example:

Say that I want to do:

$ cd /some/path/to/a/directory

but as I type it it becomes like this:

$ cd /some/pa/directoryath/to

if I type enter, it will still read the command correctly, it is just the display that is wrong. When it happens, if I try to use the backspace it will turn into something like this:

$ cd /some/pa/directoryath/to
$ cd /some/pa/directo
$ cd /some/pa     -------- at this point I cannot erase anything else,
                           in the display some of the characters are still 
                           lingering, but if I type enter there is nothing on the input line

Another example, say that I have the following displayed:

$ ls
folder1 folder2 folder3 folder4

and now I want to type some other command, then the following might happen:

$ ls
folder1 folder2 folder3 folrectory
$ cd /some/path/to/a/di

Once again, if I type enter the input is recognized correctly, but the display is screwed up.

The issue occurs most frequently, but not exclusively, when I type the up arrow to get a previous command, or when the input line approaches the right edge of the window.

Best Answer

You need to enclose the terminal escape sequences, and only those, between \[ and \].

See in bash's manual page, under the "PROMPTING" section.

Instead of

PS1='\e[1;36m\u@\h \W]\$\e[0m '

you should write

PS1='\[\e[1;36m\]\u@\h \W]\$\[\e[0m\] '
Related Question