为什么我的Bash提示颜色使光标出现在错误的位置 [英] Why do my Bash prompt colors make cursor appear in wrong spot

查看:79
本文介绍了为什么我的Bash提示颜色使光标出现在错误的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下bash提示:

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:

我知道问题与提示的长度有关,而不是bash期望的,但我是一个完整的bash菜鸟.我该怎么做才能解决此问题?

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?

推荐答案

ps1c 的输出不应计入提示的长度,因此必须将其包装在 \ [... \] .

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\]'

这篇关于为什么我的Bash提示颜色使光标出现在错误的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆