ANSI color escapes in PS1

Extracted from debian-list
Tip provided by Marcus Brinkmann
> 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?
> 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.
What the other said, is correct, so you already know what is happening here.
However, here is just another example for you:
# ~/.bash_profile: executed by bash(1) for login shells.
   set_prompt ()
     local SAVE_CRS=`tput sc 2> /dev/null`
     local RESET_CRS=`tput rc 2> /dev/null`
     local CLOCKPOS=`tput cup 0 $(($HZ-10)) 2> /dev/null`
     local FOREG=`tput setaf 6 2> /dev/null` #4
     local ALT_FOREG=`tput setaf 3 2> /dev/null` #4
     local BACKG=`tput setab 0 2> /dev/null` #6
     local NORMAL=`tput sgr0 2> /dev/null`
     local BOLD=`tput bold 2> /dev/null`
 ${RESET_CRS}${BOLD}${ALT_FOREG}\]\u\[${NORMAL}\]@\h:\w\$ "
This is will display a yellow username, (in normal colors) "@" + hostname, ":" +
path, "$" and a cyan clock in the upper right corner, 10 columns from the line end.

The clock will only be updated when you enter a new command, though (or
press return). The clock is annyoing when scrolling up or cutting and
pasting. However, it is just a demonstration how to do cursor movements.

Note that you don't need to enter _any_ escape commands, because I use tput
to get them. See "man 5 terminfo" for a list of terminal capabilities.

From Daniel Martin :

"tput" is a command-line interface into the terminfo database - if
your terminal is known to be able to handle colors, it'll generate
escape codes and if not it'll generate nothing.  Thus, this prompt
line works just as well on a color-capable xterm as it does on an old
Wyse terminal (which not only can't do color, but barfs if handed
VT-style escape codes).

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

Appears in section(s) : screen configuration shell
Tip recorded : 07-12-1998 21:22:18
HTML page last changed : 27-07-1999 20:10:47