Many people use the current directory in their prompts. If you use the pushd and popd (Section 30.7) commands, you may not always remember exactly what's in your directory stack (I don't, at least). Here's how: run the dirs command, and use its output in your prompt. A simple csh and tcsh alias 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 or .tcshrc to make a multiline prompt (Section 4.7) that shows the directory stack:
uname -n Section 2.5, expr Section 36.21
# PUT hostname.domain.name IN $hostname AND hostname IN $HOST: set hostname=`uname -n` setenv HOST `expr $hostname : '\([^.]*\).*'` alias setprompt 'set prompt="\\
Go to http://examples.oreilly.com/upt3 for more information on: dirs-prompt.cshdirs-prompt.sh
${USER}@${HOST} `dirs`\\ \! % "' alias cd 'chdir \!* && setprompt' alias pushd 'pushd \!* && setprompt' alias popd 'popd \!* && setprompt' setprompt # SET THE INITIAL PROMPT
Because bash can run a command each time it sets its prompt, and because it has built-in prompt operators (Section 4.3) like \u, the bash version of all the previous stuff fits on one line:
$(...) Section 28.14
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 %
Of course, the prompt looks a little redundant here 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, and edit it with a command like sed or with the built-in csh string editing (Section 28.5).
For example, to show just the tail of each path in the dirs output, use the following alias; the C shell operator :gt globally edits all words, to the tail of each pathname:
Go to http://examples.oreilly.com/upt3 for more information on: dirstail-prompt.csh
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 ~
--JP and SJC
Copyright © 2003 O'Reilly & Associates. All rights reserved.