[wplug] colored prompt?
Logan
ascii at psu.edu
Fri Feb 23 22:31:38 EST 2007
> I am using BASH and would like to know how I can get a colored
> prompt working?
>
> I don't need the entire prompt colored just the fist part which shows
> user at host ~>
There are escape sequences to change the color, position of the
carrot, etc
So I set up some shell variables in my ~/.bash_profile
WHITE="\e[1;37m\]"
BRIGHTGREEN="\e[1;32m\]"
GREEN="\e[0;32m\]"
CYAN="\e[0;36m\]"
GRAY="\e[0;37m\]"
DEFAULT="\e[0m\]"
RED="\e[0;31m\]"
("\e" is "escape" in BASH, but isn't standard ANSI)
thus these are called "escape sequences"
if you just want your prompt to change color, you'd do something like:
PS1="\[${GREEN}\]\u@\h \[${DEFAULT}\]"
"\[" tells bash "the following shouldn't be counted as printing
characters when calculating when to wrap text, and "\]" tells it
you're back to printing characters
I go crazy, and change the title bar to show my host and PWD, as I am
usually SSHing about
case $TERM in
xterm*)
TITLEBAR='\e]0;\h:\w\007\]\r'
;;
cygwin*)
TITLEBAR='\e]0;\h:\w\007\]\r'
;;
*)
TITLEBAR=''
;;
esac
then, have different prompts for error and non-error
function tranquil_thoughts_prompt {
PS1="\[${TITLEBAR}${WHITE}\].oO(\[${GREEN}\]\u\[$
{BRIGHTGREEN}\]@\[${GREEN}\]\h \[${CYAN}\]\w\[${WHITE}\])\[${DEFAULT}
\] "
}
function error_prompt {
PS1="\[${TITLEBAR}${RED}\]\u@\h \w [exit $last] :-(\[$
{DEFAULT}\] "
}
PROMPT_COMMAND='last="$?"; if [ $last -eq 0 ]; then
tranquil_thoughts_prompt ; else error_prompt ; fi';
Where PROMPT_COMMAND is evaluated just before printing the prompt
$? is the exit status of the last command (ex: perl -e 'exit 5;')
here are some more BASH escapes for fun and profit:
\u is username
\h is host
\w is current directory
\$ is $ unless you're root - then it's #
\d is date (Wed Sept 04)
\j is number of suspended processes (w/ ^Z)
\n is newline
\r is carriage return
\t is 24-hr time format
\T is 12-hr time format
\@ is 12-hr time format w/am-pm
> Also I would like for this to work both in xterms and gnome-terminals
> in X Windows
> as well as in console ttys.
these escape codes are from the VT100,
so they'll work on any VT100 compliant terminal,
which would be everything I've ever seen from Putty to Cygwin to
Xterm to Terminal.app to ... etc
for more colors, and blinking/underline/etc search for "VT100 escape
codes" on Google or somewhere
If you want, I can send you my .bash_profile - it's got some other
interesting things in there like odd environment variables and
`shopt` shell configuration and extra options based on OS (Linux or
Mac).
-Logan
PS: sorry for rich text, but it was getting ugly with all the code/
explanation mixing
PPS: *whew* sorry about the long-winded post, but I just love
tweaking my environment :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.wplug.org/pipermail/wplug/attachments/20070223/0b7be4fe/attachment.html
More information about the wplug
mailing list