Rohan Saxena
Rohan Saxena

Reputation: 3328

Text doesn't clear after modifying terminal prompt

Note: I am running Mac OS X Sierra but this problem occurs on Ubuntu also.

I have customised the terminal prompt as such:

export PS1="\n\[\033[1;31m\]\u 🖖  \[\033[1;32m\]@ \[\033[1;32m\]\h \[\033[0;35m\]in \[\033[0;36m\]\w\n\[\033[0;34m\]> \[\033[1;37m\] \e[0m"

Note that I have put a newline at the end, so I start typing commands on a new line after the >. I have done this on a number of machines, and I've noticed that whenever I add the newline, the terminal behaves weirdly.

By weirdly, the exact behaviour I refer to is this (this includes the steps to replicate the error if you use the PS1 value I have mentioned):

  1. Access older commands by pressing the up arrow key.

  2. When I encounter a command in the history which consists of more than 1 word, and the first word is is more than 4 characters long, then the first 4 characters of the word 'stick' to the initial part of the prompt.

  3. This 'sticky part' cannot be deleted by me, and does not even go when I press the up arrow key several more times.

For instance, if the last 4 commands I entered were (from least recent to most recent): clear, man man, this that and help.

Then, when I look at previous commands by pressing the up key incrementally:

enter image description here

help is visible properly.

enter image description here

this that is visible properly.

enter image description here

Note how this is appended at the start. I cannot delete it if I try.

enter image description here

Continues to stay as I press the up key.

How can I resolve this issue?

Upvotes: 0

Views: 607

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54505

Each of the escape-sequences in your prompt has to be bracketed with \[ and \] to tell bash that those characters should be ignored for the purpose of counting columns. The last one in your example is not bracketed:

export PS1="\n\[\033[1;31m\]\u 🖖  \[\033[1;32m\]@ \[\033[1;32m\]\h \[\033[0;35m\]in \[\033[0;36m\]\w\n\[\033[0;34m\]> \[\033[1;37m\] \e[0m"

i.e., \e[0m

If you fix the error, bash is likely to give better results. This shows the suggested correction;

export PS1="\n\[\033[1;31m\]\u 🖖  \[\033[1;32m\]@ \[\033[1;32m\]\h \[\033[0;35m\]in \[\033[0;36m\]\w\n\[\033[0;34m\]> \[\033[1;37m\] \[\e[0m\]"

Upvotes: 2

Related Questions