[英]Bash prompt does not wrap text properly
I recently added some color to my bash prompt, and now when I type a command the text doesn't wrap properly.我最近在我的 bash 提示中添加了一些颜色,现在当我键入命令时,文本无法正确换行。
Here is an example:这是一个例子:
MacBook-Pro:~ williamgiles$
long commandwilliamgiles$ this is a really long command,this is a really
My original PS1:我的原始 PS1:
\h:\W \u\$
My new one with color:我的新颜色:
\e[0;35m\h:\W \u$ \e[m
You need to mark the color-changing sequences as something that does not contribute to the length of the prompt on-screen.您需要将变色序列标记为不会影响屏幕提示长度的内容。
PS1='\[\e[0;35m\]\h:\W \u$ \[\e[m\]'
bash
already knows to take the expansion of \h
, \W
, and \u
into account; bash
已经知道将\h
、 \W
和\u
的扩展考虑在内; it has no idea that \e[0;35m
is not simply 7 characters that the terminal will display on screen.它不知道\e[0;35m
不仅仅是终端将在屏幕上显示的 7 个字符。
Adding to @chepner's answer, I personally use \1
and \2
in my .inputrc
添加到@chepner 的回答中,我个人在我的.inputrc
中使用\1
和\2
Here's an example:这是一个例子:
set vi-ins-mode-string \1\e[38;5;015m\2╰─\1\e[0m\2\1\e[38;2;191;254;33m\2(ins)⫸\1\e[0m\2\1\e[5 q\2\1\e]12;rgb:bf/fe/21\e\\\2
set vi-cmd-mode-string \1\e[38;5;015m\2╰─\1\e[0m\2\1\e[38;2;191;121;33m\2(cmd)⫸\1\e[0m\2\1\e[2 q\2\1\e]12;rgb:bf/79/21\e\\\2
\1
denotes the start of a zero-length section, and \2
denotes the end. \1
表示零长度部分的开始, \2
表示结束。 ( \001
and \002
also work) ( \001
和\002
也可以)
I wrote a blog post about the topic not long ago.不久前我写了 一篇关于这个话题的博文。 You might find it interesting.你可能会觉得很有趣。
What my shell ends up looking like (The top part is possible by using Oh-My-Posh :我的 shell 最终看起来像什么(顶部部分可以通过使用Oh-My-Posh 实现:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.