When I use my terminal (iTerm 2 Mac) with my PS1 set to "\[\e[38;5;117m\W \e[39;38;5;104m\$\e[39;0m\] "
and I use the arrow keys to go through my bash history it sometimes changes my prompt from ~ $
to just the first character of it and whatever command I'm looking at. For example, going to rvim .bashrc
from randomDir $ ls
. This problem also persists in the default terminal app.
Asked
Active
Viewed 394 times
2

Folded-Code
- 49
- 4
1 Answers
3
\W
and \$
should not go inside the \[...\]
, since bash
will know how much space each takes up on the terminal.
PS1="\[\e[38;5;117m\]\W \[\e[39;38;5;104m\]\$\[\e[39;0m\] "
Only the characters that make up the ANSI escape sequence (which only instruct the terminal to change colors, without displaying a single additional character) are enclosed in \[...\]
.
Putting them inside \[...\]
tells bash
to ignore their contribution to the length of the prompt, leading to incorrect redraws.

chepner
- 497,756
- 71
- 530
- 681