ANSI color escapes in PS1

Extracted from debian-user
Tip provided by Chuck Stickelman
> Just how stupid an idea did I have when I did this:
>
> $PS1="[\e[31m\h\e[m:\e[34m\u\e[m:\e[31m\w\$\e[m]"
>
> in my /etc/profile?
>
Not stupid at all!!

> It looks really nifty until I try to do commands that wrap around, in which
> case the first line returns, but keeps going on the current spot.
>
> The real disaster, however, occurs when I get into the command buffer...
> each time I hit the left arrow, it smears about half the command up a line.
>
You need to "escape" the escape sequences - like this:

export PS1="\[^[[33;1m\]\u@\h\[^[[0m^[[1m\]: "
export PS2="\[^[[33;1m\]continue \[^[[0m^[[1m\]> "


[NOTE: I've replaced the ESCAPE character with "^["]

 
The \[...\] around the ANSI ESCAPE SEQUENCES keeps them from being counted
as part of the prompt's length.

==========
Note also the following:


> I don't quite get it... I tried replacing my \e's with ^['s and surrounding
>  all escapes with /[/] pairs, and it wouldn't work at all (just the
> "source" were displayed.. IE the same as typing `echo $PS1`)
> 
I'm sorry.  I wasn't as clear as I should have been.
the two-char ^[ was used in the e-mail message, where-as the single-char
^[ (ie. Ctrl-[) is used in the PS1/PS2 strings.  You use of \e has the same
effect as a ^[.

> I then changed the \e's back, and this time it showed color but wrapped
> funny again... what am I doing wrong?
> 
This works for me, though I do have /etc/inputrc with:

        # Use a single line for display
        set horizontal-scroll-mode on

This causes the prompt to scroll off the left edge as I type long commands.


Follow-up : (1)  
| Previous | Next | Index of category | Main Index | Submit |


Appears in section(s) : screen configuration shell
Tip recorded : 07-12-1998 20:41:58
HTML page last changed : 27-07-1999 20:06:33