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

Re: Colorized Prompts Problem

Quoting Thomas H. George (lists@tomgeorge.info):
> On Mon, May 04, 2015 at 06:54:40AM +0000, Bonno Bloksma wrote:
> > Hi,
> > 
> > > I entered the following in .bashrc
> > > 
> > >    PS1='\033[01;33m\h:\w\$ \033[00m'       
> > > 
> > > to colorize the prompt (very handy to find the prompt when a command
> > > fills the console screen with lines of text)
> > > 
> > > The only problem occurs when the next entry is more than one line.  In that
> > > case the entry wraps around without moving to a new line.
> > 
> > I had the same problem using the prompt I found at first, I think it is the same you are using. It seems there is a problem in closing the ANSI code string.
> > Someone else gave me this:
> >   PS1='\[\e[0;31m\]${debian_chroot:+($debian_chroot)}\h:\w\$\[\e[m\] '
> > This does not have the problem, I have been using this now for over a year, no problems at all.
> > 
> > Bonno Bloksma
> > 
> Thank you, this works while nothing else did.  The sequences to start
> and end coloring are different and the colors are different too.

Perhaps it's worth deconstructing this into its constituents:

'\[\e[0;31m\]${debian_chroot:+($debian_chroot)}\h:\w\$\[\e[m\] '

Take out ${debian_chroot:+($debian_chroot)}
As explained before, this prints the value of $debian_chroot but uses
the :+ trick to avoid printing () when $debian_chroot is empty/unset.

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

Take out the two \[ and \] brackets.
As explained before, these brackets must enclose anything that doesn't
advance the cursor, ie the colouring strings. That tells bash to
ignore those strings in calculating how much room the prompt has taken
up. That's the fix for your original complaint.

'\e[0;31m\h:\w\$\e[m '

Take out \h:\w\$ and that space at the end.
That's the prompt that you want to see coloured. (In your original,
the space at the end of the prompt was coloured too.)

'\e[0;31m' and '\e[m'

Now all that remains is colour-on and colour-off.

Off: Where you had \033[00m this has \e[m because the 00 before the
m is unnecessary (blank is zero), and \e is a clearer way of writing
the ESCape character (octal 33) in more modern versions of bash.
(The latter eliminates one source of confusion: the mixture of octal
and decimal numbers.)

On: Similarly, where you had \033[01;33m this has \e[0;31m which,
as pointed out below, changes the foreground colour to normal red.
So you should set it back to \e[1;33m

It is harmless, but that leading 0 that you had in 01;33m could be a
cause for confusion; the numbers before the m are *decimal* whereas
\033 is octal. So 33m actually means 30 + 3 m where 30 is the
foreground colour, 3 is the yellow, and m is colouring rather than,
say, moving the cursor.


Reply to: