gpt4 book ai didi

bash - 为什么这个 bash 提示符表现得很奇怪/消失,我该如何修复它(OS X)?

转载 作者:太空宇宙 更新时间:2023-11-04 03:50:17 26 4
gpt4 key购买 nike

我承认我使用了有点冗长的 bash 提示符:

--(username)-(Wed April 01|12:00:00)--(~ $

最近,我有了一个好主意,将其更改为根据上一个命令的退出值,如果成功, () 的内部元素将是绿色的,如果失败,它们将是红色的。我让它在大多数情况下都能工作(一些奇怪的退出状态会将颜色更改为其他颜色,但我对此表示同意),但是当输入超过一行的命令并导致终端滚动时,提示消失了!当没有颜色时,我的提示工作正常,所以我猜测这与我的颜色转义有关,特别是我未封闭的 [ 的,但我无法确定它。

#.profile
export PS1='--(\e[$((32-${?}))m\u\e[0m)-(\e[$((32-${?}))m\d\e[0m|\e[$((32-${?}))m\T\e[0m)--(\e[$((32-${?}))m\w\e[0m \$ '

提前致谢!

最佳答案

听起来像this应该可以解决你的问题。

这似乎对我有用*:

export PS1='--(\[\e[$((32-${?}))m\]\u\[\e[0m\])-(\[\e[$((32-${?}))m\]\d\[\e[0m\]|\[\e[$((32-${?}))m\]\T\[\e[0m\])--(\[\e[$((32-${?}))m\]\w\[\e[0m\] \$ '

* 嗯,确实 export PS1='\u@\h:\w\$ ' 对我有用

要引用链接的帖子,答案在于在 PS1 声明中的所有颜色序列周围添加 \[ 和\]:

Before I had the following value for PS1:

'\e[0;34m\h:\w [!]\$\e[0m '

which gave me a nice blue prompt of the following form

hostname:working-directory [command-number]$

However, I had the same line-wrapping problem you did. The fix was to insert \[ and \] around the ANSI escapes so that the shell knows not to include them in the line wrapping calculation. This results in the following value for PS1:

'\[\e[0;34m\]\h:\w [!]\$\[\e[m\] '

关于bash - 为什么这个 bash 提示符表现得很奇怪/消失,我该如何修复它(OS X)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26589802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com