问题
I've got the following bash prompt:
# helper function to set colors
function ps1c() {
tput setaf $1;
}
PS1='$(ps1c 243)\h $(ps1c 177)\W $(ps1c 214)$ \[\e[m\]';
When typing, sometimes my cursor returns to the left side of the screen and looks like it's overwriting the prompt. Also, when using the up arrow to navigate through my history, things get weird, and the cursor goes all over the place.
I've checked the following, but they don't really provide a clear answer on how to fix the issue:
- Why does my bash prompt sometimes get overwritten?
- why isn't my bash prompt properly escaping?
I know the problem has something to do with the length of the prompt not being what bash expects, but I am a complete bash noob. What can I do to fix this?
回答1:
The output of ps1c
should not be counted towards the length of your prompt, so it must be wrapped in \[...\]
as well.
PS1='\[$(ps1c 243)\]\h \[$(ps1c 177)\]\W \[$(ps1c 214)\]$ \[\e[m\]'
来源:https://stackoverflow.com/questions/42121662/why-do-my-bash-prompt-colors-make-cursor-appear-in-wrong-spot