仅在键入字符后退格擦除 bash 提示 [英] Backspace erasing bash prompt only after typing characters

查看:60
本文介绍了仅在键入字符后退格擦除 bash 提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是新的 linux 用户.

New linux user here.

我的 bash 提示如下:

My bash prompt looks like this:

export PS1=$"\[\e[1;32m\d \t \e[1;33mฅ^\e[m\e[1;36mᵒ\e[m\e[1;33mﻌ\e[m\e[1;36mᵒ\e[m\e[1;33m^ฅ \e[1;35m\W\e[1;32m$\]"

(这是一只小猫ฅ^ᵒﻌᵒ^ฅ)

现在我已将 ] 添加到字符串的末尾,当提示为空时,它不再删除我的 bash 提示字符.但是,一旦我开始输入命令然后按退格键,它就会删除我的 bash 提示符.

Now that I've added the ] to the end of the string, it no longer deletes my bash prompt characters when the prompt is empty. However, it does delete my bash prompt once I start typing a command and then press backspace.

有没有办法防止这种情况发生,或者这只是一个bash的事情?

Is there a way to prevent this, or is this just a bash thing?

谢谢!

推荐答案

\[...\] 表示包含的部分不占用提示中的任何空间.因此,它们将用于 xterm 控制序列(\e[1;32m 等),而不是提示文本(\d\t 等).尝试类似:

The \[...\] indicates that the contained part does not take up any space in the prompt. So, they're to be used for the xterm control sequences (\e[1;32m, etc.), not the prompt text (\d, \t, etc.). Try something like:

PS1='\[\e[1;32m\]\d \t\[\e[1;33m\]ฅ^\[\e[m\e[1;36m\]ᵒ\[\e[m\e[1;33m\]ﻌ\[\e[m\e[1;36m\]ᵒ\[\e[m\e[1;33m\]^ฅ\[\e[1;35m\]\W\[\e[1;32m$\]'

如果您的整个提示都包含在 \[...\] 中,那么 bash 会认为您的提示根本不占空间,并从该行的开头开始输入.

If your entire prompt is wrapped in \[...\], then bash thinks your prompt takes no space at all, and typing begins at the start of the line.

(顺便说一下,您不需要导出PS1.)

(You don't need to export PS1, by the way.)

这篇关于仅在键入字符后退格擦除 bash 提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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