T. Joseph CARTERFri Dec 31 23:21:16 PST 2004
On Thu, Dec 30, 2004 at 08:35:08AM -0800, Jeff_W wrote: > 3.4.3 Colour on text terminal [..] > > Colour on the text terminal can be produced using the "ANSI escape > sequences". For example: > > echo -e "\033[44;37;5m ME \033[0m COOL" Under bash, you can substitute "\033[" with "\e[", in case you don't feel like typing out the escape character's octal representation. [..] > Code Action/Color > --------------------------- > 0 reset all attributes to their defaults > 1 set bold > 2 set half-bright (simulated with color on a color display) > 4 set underscore (simulated with color on a color display) > 5 set blink > 7 set reverse video > 22 set normal intensity > 24 underline off > 25 blink off > 27 reverse video off [..] Note that codes are generally put in the order you want them applied. Underscore on a VGA colour device is handled by setting the text to dark blue. Not all terminals handle reverse properly when combined with colours other than the default. Also, while the Linux console and xterm will do blink, some terminals use the blink attribute to enable "bright" background colours, working just like the bright foreground colours. rxvt-based terminals (including eterm and aterm and wterm) all have this feature, as can the Linux framebuffer console. The details are somewhat obscure if you're not used to boolean logic and the VGA text screen. > 38 set underscore on, set default foreground color > 39 set underscore off, set default foreground color [..] > 49 set default background color Wouldn't count on these on many terminals. They're part of the DEC standards, but keep in mind that you'd have been expected to pay for the privelege of reading the standards. If you have to ask how much, you can't afford it. ;) > Other interesting codes: > > \033[2J clear screen ANSIism! Does not work under term type "linux" (the Linux console). Use \033[H\033[J instead. H without numbers really means 1;1H and J without numbers means clear to end of screen. Most terminals that speak MS-DOS ANSI can handle 1J (which clears something I have long forgotten) and 2J, but the Linux console cannot. > \033[0q clear all keyboard LEDs (won't work from Xterm) > \033[1q set "Scroll Lock" LED > \033[2q set "Num Lock" LED > \033[3q set Caps Lock LED Don't count on these even outside xterm, they're not exactly standard. > \033[15;40H move the cursor to line 15, column 40 > \007 bell (beep) Let me add one, and maybe explain how people use these codes a bit: \033[Some text\007 Sets "Some text" as an xterm title That should only be used in xterm or compatible. You're on your own with gnome-terminal, but even Gnome people would realize they should make this code work. The current standard way to use it goes something like this, in your ~/.bash_profile (assuming bash, once again): if [ "$TERM" = "xterm" -o "$TERM" = "xterm-color" ] ; then PROMPT_COMMAND='echo -e "\e[\u@\h:\w\007"' else PROMPT_COMMAND= fi Don't try to put colours or other codes into an xterm title. If you want to put colour into your bash prompt, you can do so by modifying the PS1 variable. Chances are good that your distribution already sets this to something sane like "\u@\h:\w\$ " which expands to your username, an @, your hostname, your working directory, and either $ or #, depending on whether or not you have superuser priveleges. There are other backslash codes, but these are all you probably want to know, except maybe \W, \[, and \]. Let me explain real quickly about \w and \W. Let's say I'm me, and I'm in my home directory: knghtbrd at yumi:~$ pwd /Users/knghtbrd knghtbrd at yumi:~$ _ Okay, that'd be /home rather than /Users on a "normal" system, but I have a Mac and have never been accused of normality. ;) My prompt shows a \w after the : above, and my home directory is abbreviated as a squiggle. knghtbrd at yumi:~$ PS1="\u@\h:\W\$ " knghtbrd at yumi:knghtbrd$ _ Note that knghtbrd is both my user name and the name of the directory I'm sitting in. knghtbrd at yumi:knghtbrd$ cd Desktop knghtbrd at yumi:Desktop$ PS1="\u@\h:\w\$ " knghtbrd at yumi:~/Desktop$ _ As you can see, \W shows just the current directory (after the last /) and \w shows your full current directory, abbreviating your home dir as a ~. You could add colour in between these escape codes in setting your PS1, but bash needs to know what in your prompt is printed to the screen and what is not. Anything following \[ will be considered to take "no space" on the screen (ie, it's a control code) until bash sees \]. Try this (preferably on a black background terminal): knghtbrd at yumi:~$ PS1="\[\e[33;1m\]\u@\h:\w\[\e[0m\]\$ " knghtbrd at yumi:~$ _ Let me expand that out so it's a little easier to read: "\[ \e[33;1m \] \u @ \h : \w \[ \e[0m \] \$ " You can see how the \[ \] pair encapsulates the color codes. Setting your PS1 as described above will give you a "bright brown" (ie, yellow) prompt which changes back to normal for the $ or #. You can get very complex with these codes, provided that you encapsulate your codes properly. For example, let's have a look at my normal prompt on yumi: knghtbrd at yumi:~$ echo $PS1 \[\e[0;36m\]\u@\h\[\e[36;1m\]:\[\e[0m\]\w\[\e[36;1m\]\$ \[\e[0m\] That's cyan \u@\h, a bright cyan :, normal \w, a bright $ or #, and resetting to normal for what I type. My root account has a different prompt: \[\e[0;31;1m\]\h\[\e[36;1m\]:\[\e[0m\]\w\[\e[36;1m\]\$ \[\e[0m\] This is different in that there is no \u@ and \h is done in bright red instead of dark cyan. It's just a bright visual cue that I'm using a root account so that I'm less likely to accidentally break something I will be spending the next week trying to undo. Consequently I colour-code my system prompts because I ssh between them so often. My workstation, yumi, is cyan/bright cyan. My laptop uses normal and bright magenta. I've had machines using normal and bright green, yellow and white, and cya and bright blue. Quite effective for knowing which machine I'm typing "shutdown -r now" into. ;)