[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Colorized Prompts Problem - Thanks for the Deconstruction



Quoting Chris Bannister (cbannister@slingshot.co.nz):
> On Fri, May 15, 2015 at 09:49:12PM -0500, David Wright wrote (and corrected himself):

> > This conversation made me revisit my own prompt which I have now
> > modified. I thought I'd share it with you in case any part of it
> > should be helpful.
> > 
> > LOCAL_COLOR is set in a host-specific file that also sets things like
> > different colours for midnight commander (mc) so I know which host I'm on.
> > 
> > PROMPT_COMMAND adds a space after the return code and then removes
> > itself if it would print zero. It also sets the title-bar in an xterm,
> > again so I know where I am. (I have a couple of dozen xterms, some
> > ssh'd to other hosts.) Putting the tty number in the title bar makes
> > it easier to kill an xterm should it freeze.
> > 
> > PS1 makes any non-zero return code stand out; then the local colour
> > takes over the rest of the prompt. Note that the "0;" is required in
> > LOCAL_COLOR to cancel the earlier "1;" that highlights the return code.
> > Breaking the PS1 line into 3 parts avoids another level of quoting.
> > 
> > 
> > LOCAL_COLOR='\e[0;34m' # blue (this is in ~/.bash-<hostname>)

I was economical with the truth to make the posting simpler.

.bashrc has . .bash-1-<hostname> near the top (and . .bash-9-<hostname>
near the bottom) to set host-specific parameters and functions.

.bash-1-<hostname> in turn has . .bash-c-<colour> to set its desired
colours in LOCAL_COLOR and MC_COLOR_TABLE. (The latter accounts for
the spelling of the former.)

> > export PROMPT_COMMAND='MYPROMPT="$? " && [ "$MYPROMPT" = "0 " ] && MYPROMPT=""'
> > case $TERM in
> >     xterm*)
> > 	export PROMPT_COMMAND+=" ; echo -ne '\e]0;${HOSTNAME^^}          $(tty)          ${HOSTNAME^^}\a'"
> > 	;;
> > esac
> > 
> > export PS1='\[\e[1;33;41m\]$MYPROMPT\['
> > export PS1+="$LOCAL_COLOR"
> > export PS1+='\]\H!\u \t \w \$ \[\e[m\]'
> > unset LOCAL_COLOR
(I've corrected "||" in the original posting to ";".)

> Is all that in one file? Is it sourced via .bashrc?

So not one file, but the lines in the last section quoted above *are*
the very last lines in ~/.bashrc. Why last? To avoid my startup files
printing anything with non-interactive shells, which they detect by
PS1 being empty/unset.

The only other slightly relevant change I made was to add the line

:

to ~/.bash_profile in order to prevent one getting a highlighted
return code on logging in, eg, if the last command is a grep that
matches nothing.

Cheers,
David.


Reply to: