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 expr | # 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' 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 (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.
-