Why do my Bash prompt colors make cursor appear in wrong spot [duplicate]

 ̄綄美尐妖づ 提交于 2021-02-17 03:22:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!