Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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: alt text
(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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top