[GLLUG] bash PS1 commands...

C. Ulrich dincht@securenym.net
Fri, 16 May 2003 01:54:43 -0400


Matt Graham wrote:
> Several folks asked about customizing the prompt when using bash.

Some post-meeting thoughts:

1. On my machines, the prompt consists of the following (without quotes):

"[\[\033[1;33m\]\h\[\033[0m\]:\w]\$ "

This prints a yellow hostname, a colon, and the working directory, all
enclosed within brackets. (With a $ tacked on for good measure.) The brackets
help visually discriminate between prompts and program output at a glance. The
yellow in the hostname does the same thing and is especially nice when trying
to find the start of some program output in scrollback that's hundreds or
thousands of lines long.

You can do the xterm titlebar stuff from within the PS1 prompt too. (Rather
than hacking it into PS1, Matt showed us how to do this with PROMPT_COMMAND.
Is there any distinct advantage to using that over PS1 for this sort of
thing?) You can put it pretty much anywhere within the prompt string, but I
put it on the end:

"[\[\033[1;33m\]\h\[\033[0m\]:\w]\$ \[\033]0;\u@\h:\w\007\]"

That puts "user@host:path" in the xterm titlebar. Caveat: it makes a normal
non-xterm terminal (a virtual console, for instance) beep and print the
extraneous stuff. Solution? A bit of clever scripting. The environment
variable TERM should contain the string "xterm" somewhere within it (mine says
"xterm-color"), so we just check for that upon login and then modify PS1, or
not, accordingly:

export PS1='[\[\033[1;33m\]\h\[\033[0m\]:\w]\$ '
if [ $(echo $TERM | grep -c xterm) -gt 0 ]; then
    export PS1="${PS1}\[\033]0;\u@\h:\w\007\]"
fi

There's a really helpful Bash Quick Reference PDF that's apparently meant to
be printed out and cut up into actual quick-ref cards. Googling shows this to
be the only surviving copy:

http://www.digilife.be/quickreferences/QRC/Bash%20Quick%20Reference.pdf

Switching gears to Vim, some of the newer Linux users may be wondering why
they'd want to bother learning vim over one that's easier to use but lacking
in flexibility and power. I could hash out my personal life's story as it
pertains to text editors but suffice to say for now that like Unix itself,
learning Vim is one of those activities where you have to climb up the
learning curve quite some way before you start to see the benefits, but once
you're up there you find that the journey paid off big time. I used Nano
almost exclusively for 5 years and live to regret it. :P

Some great docs for Vim can be found at the official Vim web site, vim.org.
Specifically, look for the PDF version of The Vim Book by Steve Oualline.
O'Reilly reportedly has a good Vi book also. Finally, there's the Vi Lovers
Home Page which is a good collection of links, web pages, and discussion of Vi
in general: http://www.thomer.com/vi/vi.html

--Charles Ulrich
-- 
http://bityard.net