回顾/编辑命令时添加的ANSI颜色转义序列,以在恶劣的光标位置bash提示符结果 [英] Adding ANSI color escape sequences to a bash prompt results in bad cursor position when recalling/editing commands

查看:337
本文介绍了回顾/编辑命令时添加的ANSI颜色转义序列,以在恶劣的光标位置bash提示符结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我把我的命令提示符下这样的:

If I set my command prompt like:

export PS1='\033[0;33m[\u@\h \w]\$ \033[00m'

提示符的颜色为黄色,$字符后一切都会
是默认的终端颜色。这是我的期望。但是,如果我还记得一个命令行并尝试对其进行编辑,移动光标 - 要么UPARROW /按Ctrl-A(设定-o emacs的)或ESC K(设定-o六)
如果我想编辑命令行足够长时,光标定位不是命令的开始。无论是打字按Ctrl-A(设定-o emacs的)或者^(设置-o六)不会将光标移动到我所看到的在屏幕上召回行的开头。同样,在试图将光标定位到行(CTRL-E或$,取决于)在其结果的末端被放置几个字符过去似乎是该行的末尾。
它看起来像bash的是越来越被我添加到提示的转义字符混淆。
这只是我不得不处理,改变了我的提示单色之一,当我想编辑回忆说行,还是有办法让bash的正确允许召回命令的编辑与丰富多彩的提示?

The color of the prompt will be yellow and everything after the '$' character will be the default terminal color. This is what I expect. However, If I recall a command line and attempt to edit it, moving the cursor -- either UpArrow/Ctrl-A (set -o emacs) or ESC K (set -o vi) if the command line I'm trying to edit is long enough, the cursor is not positioned at the beginning of the command. Typing either Ctrl-A (set -o emacs) or ^ (set -o vi) will not move the cursor to what I'm seeing as the beginning of the recalled line on the screen. Similarly, attempting to position the cursor to the end of the line (Ctrl-E or $, depending) results in it being placed several characters past what appears to be the end of the line. It looks like bash is getting confused by the escape characters I've added to the prompt. Is this just something I'll have to deal with, changing my prompt to a monochromatic one when I wish to edit recalled lines, or is there a way to get bash to correctly allow the editing of recalled commands with a colorful prompt?

推荐答案

您需要附上非打印字符 \\ [... \\] ,使庆典知道忽略他们的计算提示的长度时:

You need to enclose the non-printing characters in \[ ... \] so that bash knows to ignore them when computing the length of the prompt:

export PS1='\[\033[0;33m\][\u@\h \w]\$ \[\033[00m\]'

这篇关于回顾/编辑命令时添加的ANSI颜色转义序列,以在恶劣的光标位置bash提示符结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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