简体   繁体   中英

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:

在此处输入图片说明

help is visible properly.

在此处输入图片说明

this that is visible properly.

在此处输入图片说明

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

在此处输入图片说明

Continues to stay as I press the up key.

How can I resolve this issue?

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"

ie, \\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\]"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM