UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 7.10 What Good Is a Blank Shell Prompt? Chapter 7
Setting Your Shell Prompt
Next: 7.12 External Commands Send Signals to Set Variables

7.11 dirs in Your Prompt: Better than $cwd

The C shell gives the absolute pathname of your current directory in $cwd (14.13). Many people use that in their prompts. If you use the pushd and popd (14.6) commands, you may not always remember exactly what's in your directory stack (I don't, at least). Also, do you want to shorten your home directory pathname to just a tilde (~) so it takes less room in the prompt? Here's how: run the dirs command and use its output in your prompt. A simple alias for cd users looks like this:

alias cd 'chdir \!* && set prompt="`dirs`% "'

and the prompts look like:

/work/project % cd
~ % cd bin
~/bin %

Here's what to put in .cshrc to make a multiline prompt (7.5) that shows the directory stack:

uname -n 

# PUT hostname.domain.name IN $hostname AND hostname IN $HOST:
set hostname=`uname -n`
setenv HOST `expr $hostname : '\([^.]*\).*'`

alias setprompt 'set prompt="\\
${USER}@${HOST} `dirs`\\
\! % "'
alias cd  'chdir \!* && setprompt'
alias pushd  'pushd \!* && setprompt'
alias popd  'popd  \!* && setprompt'

Because bash can run a command each time it sets its prompt, and because it has built-in prompt operators (7.4), the bash version of all the stuff above fits on one line:

PS1='\n\u@\h $(dirs)\n\! \$ '

That makes a blank line before each prompt; if you don't want that, join the first and second lines of the setprompt alias or remove the first \n. Let's push a couple of directories and watch the prompt:

jerry@ora ~
1 % pushd /work/src/perl
/work/src/perl ~

jerry@ora /work/src/perl ~
2 % cd ../cnews

jerry@ora /work/src/cnews ~
3 % pushd ~/bin
~/bin /work/src/cnews ~

jerry@ora ~/bin /work/src/cnews ~
4 %

Warning! Of course, the prompt looks a little redundant there because each pushd command also shows the dirs output. A few commands later, though, having your directory stack in the prompt will be handy. If your directory stack has a lot of entries, the first line of the prompt can get wider than the screen. In that case, store the dirs output in a shell array (47.5) and edit it with a command like sed or with the built-in csh string editing (9.6).

For example, to show just the tail of each path in the dirs output, use the alias below; the C shell operator :gt globally edits all words, to the tail of each pathname:

alias setprompt 'set dirs=(`dirs`); set prompt="\\
${USER}@${HOST} $dirs:gt\\
\! % "'

Watch the prompt. If you forget what the names in the prompt mean, just type dirs:

jerry@ora bin cnews jerry
5 % pushd ~/tmp/test
~/tmp/test ~/bin /work/src/cnews ~
jerry@ora test bin cnews jerry
12 % dirs
~/tmp/test ~/bin /work/src/cnews ~

There's a related tip in article 47.5: storing the directory stack in an array variable.

- JP

Previous: 7.10 What Good Is a Blank Shell Prompt? UNIX Power ToolsNext: 7.12 External Commands Send Signals to Set Variables
7.10 What Good Is a Blank Shell Prompt? Book Index7.12 External Commands Send Signals to Set Variables

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System