终端PS1修复中的断线 [英] Break line in terminal PS1 fix
问题描述
我有以下代码可以为终端着色:
I have this code to color my terminal:
export PS1="\e[1;30m\][\e[\e[1;30m\]\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\] \e[1;30m\]]\n[\[ \e[1;31m\]\T\[\e[0m\]\e[1;30m\] ] > \e[37m\]"
但是我有一个问题,当文本应该在新行中时,它将覆盖第一行.
But I have one problem, when text should be in the new line it overwrites the first line.
示例:
推荐答案
为了让bash找出您的提示占用了多少屏幕空间(以及实际的命令行从何处开始),您必须将非-在\[...\]
中打印提示的某些部分.通常,这意味着像\e[1;30m
这样的转义序列需要写为\[\e[1;30m\]
.您的提示符中有一些\[
和\]
,但是它们放在错误的位置,这使bash非常困惑.查找提示中所有与您一样复杂的打印和非打印部分并非易事,但我认为这样做是正确的:
In order for bash to figure out how much screen space your prompt takes up (and therefore where the actual command line starts), you have to enclose the non-printing parts of the prompt in \[...\]
. Mostly, that means escape sequences like \e[1;30m
need to be written as \[\e[1;30m\]
. You have some \[
's and \]
's in your prompt, but they're in the wrong places, which is making bash very confused. Finding all the printing and non-printing parts of a prompt as complex as yours is not trivial, but I think this gets it right:
export PS1='\[\e[1;30m[\e[\e[1;30m\e[1;33m\] \u@\H \[\e[1;32m\]\w\[\e[0m\] \[\e[1;30m\]]\n[ \[\e[1;31m\]\T\[\e[0m\e[1;30m\] ] > \[\e[37m\]'
这篇关于终端PS1修复中的断线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!