简体   繁体   中英

What in PS1 is causing my Terminal.app commands to get stuck on the screen?

When cycling through statements entered to the console, I occasionally find that the text I entered isn't refreshed and the prompt is moved to the right.

My original, intended prompt: http://cl.ly/image/04080N260L1V .

What happens after hitting the Up and Down arrows about a dozen times: http://cl.ly/image/1n3S2K31340R .

In case the screenshots aren't clear, the underlined text (in this case, " vim ~/.bas ") is getting "added" to the prompt. I can't delete it out. However, if I delete as much as I can, clearing any text after the prompt, and hit Enter, I'm greeted with my clean, original prompt again: http://cl.ly/image/2O1h1Z2y0n2I .

Here's what ~/.bash_profile contains:

# Simpler bash prompt in Terminal.app
promptColor="\e[1;34m"
endColor="\e[m"
#export PS1='\e[0;36m\w$ \e[0m'
export PS1="$promptColor\w$ $endColor"

# Syntax highlighting for commands like `ls` and such
alias ls="ls -G"

# PATH ammendment to put Homebrew-installed apps in front of system-provided ones
homebrew=/usr/local/bin:/usr/local/sbin:/usr/local/share/npm/bin
export PATH=$homebrew:$PATH

I've narrowed the culprit down to the PS1 variable. (You can see I've tried this a few different ways.) Based on what I've read, I'm using the color codes correctly.

Any help would be fantastic. Thanks.

This is a FAQ . In order for Bash to be able to compute the display length of the prompt correctly, any non-printing sequences such as color codes need to be inside a \\[...\\] sequence.

I think you want:

promptColor='\e[1;34m'
endColor='\e[m'
export PS1="$promptColor"'\w$ '"$endColor"

(Notice all the subtle changes from double to single-quotes)

The problem is that bash is doing expansion on the following when they need to be interpreted explicitly:

  • \\e[1;34m
  • \\w$
  • \\e[m

Single-quotes and double-quotes mean different things in shell: Strong Quoting vs. Weak Quoting .

I would also just copy and paste the lines with escaped characters and modify them (note that they aren't the same as literal representations)

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