Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?

谁都会走 提交于 2019-11-26 20:07:40

问题


I admit that I use a somewhat long-winded bash prompt:

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

Recently, I got the bright idea to change it so that depending on the exit value from the previous command, if success, the interior elements of the ()'s would be green, and if failure, they would be red. I got it working for the most part (some odd exit statuses will change the color to something else, but I'm ok with it), but when typing a command which is more than one line, and causes the terminal to scroll, the prompt disappears! My prompt worked fine when there was no color, so I'm guessing it is related to my color escaping, and particularly my unclosed ['s, but I can't pin it down.

#.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 \$ '

Thanks in advance!


回答1:


It sounds like this should solve your problem.

This seems to work for me*:

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\] \$ '

* well, really export PS1='\u@\h:\w\$ ' works for me

To quote the linked post, the answer lies in adding \[ and \] around all of your color sequences in your PS1 declaration:

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\] '




回答2:


http://mywiki.wooledge.org/BashFAQ/053 -- I have a fancy prompt with colors, and now bash doesn't seem to know how wide my terminal is. Lines wrap around incorrectly.

By the way; for your reference; here's my PS1 which looks like this:
(source: lyndir.com)

\[$reset$bold$green\]\u@\h\[$blue\] \W \[$red\]${?/#0/\[$green\]}\$\[$reset\]

Notice how I put all the color codes in $parameters to make it neater, but more importantly, because you should be using tput to generate them. See:

http://mywiki.wooledge.org/BashFAQ/037 -- How can I print text in various colors?

I declare my color parameters in a utility script that gets sourced by my ~/.bashrc (and any scripts I write) which is called bashlib.

On a final note; put your PS1 definition in ~/.bashrc and don't export it. There's absolutely no reason why you should add your PS1 definition to the environment of any and all processes you spawn from your shell.




回答3:


You just seem to be missing the start and end brackets around your escapes (before the first '\e' and after the last 'm'):

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\] \$ '

As mentioned, the PS1 var does not need to be exported: only your shell needs to see it.



来源:https://stackoverflow.com/questions/706750/why-is-this-bash-prompt-acting-strangely-disappearing-and-how-do-i-fix-it-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!