2

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.

bjorn
  • 23
  • I think your problem is due to how the ANSI sequences are interpreted and interact with other ways to communicate with the command line interface. I have noticed that the bug will cause problems, for example when you have a long command line, that will reach the right border of the window/screen and is wrapped. - I use this way to get a coloured prompt and I live with the problem because it helps a lot to have a coloured prompt. – sudodus Sep 15 '21 at 10:29
  • If it only occurred during wrapping, I could also live with it. But as it is now, almost every time I use the up button it breaks, and every tenth command or so, that is short, will also have issues. As it is now, it is unfortunately not usable for me... If it is a bug, however, I guess the correct approach would be to file a bug report. – bjorn Sep 15 '21 at 10:32
  • Which version of Ubuntu, bash and terminal emulator are you using? Have you tried another terminal emulator? – sudodus Sep 15 '21 at 10:34

2 Answers2

1

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\] '
egmont
  • 8,225
0

I tested and notice a problem with your prompt too, it seems to expect the right margin too early and makes a line break.

Please try this one, that I use

if [ "$USER" = root ]; then
    PS1='\[\033[01;31m\]$(statstring)\[\033[00m\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] \$ '
else
    PS1='\[\033[01;31m\]$(statstring)\[\033[00m\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w\[\033[00m\] \$ '
fi

and if it works, you can put it into your ~/.bashrc at least for interactive shells. I use

case "$TERM" in
xterm-color)
....

to decide when to use it, and you may check for something that matches your setup.


Edit: Add a function to create statstring before you set the prompt in ~/.bashrc

function statstring {
RC=$?
  if [ "0" != $RC ]; then
    printf "[$RC] "
  fi
}
sudodus
  • 46,324
  • 5
  • 88
  • 152
  • 1
    Thank you! I had to remove the $(statstring) but now it seems to be working as expected. What exactly was it in my original attempt that was causing the issue? n.b. I experienced the same issue with the suggestions in the linked question as well. – bjorn Sep 16 '21 at 08:13
  • I am not sure why your old prompt went wrong. It is different from mine at the end (the order between the $ character and the ANSI sequence), maybe that is why it goes wrong. Also, the ANSI sequences are slightly different, which may create different results. -- Please the the edited answer and try to use statstring. – sudodus Sep 16 '21 at 11:05