[comp.unix.shell] Summary: dir in csh prompt

johnb@srchtec.uucp (John Baldwin) (09/28/90)

Well, the explanations have stopped drifting in...
    ...and the torrent of requests for a summary have begun!

I haven't had time to try all the different things yet, but here is
a (partial) synopsis:

My original question was on how to get the "cwd" into my prompt in
the cshell and keep it updated.  The responses can be categorized as
follows:

Category   Response
--------   ------------------
   1       "Which shell?"      ...RTNGP, I said "csh prompt."

   2       "Use an alias."

           [This, I should have remembered enough about Unix to figure out.
           There were maybe a dozen semantically-equivalent variants.
           The general mechanism was:
              a) write an alias to update your prompt,
              b) invoke this alias on login,
              c) write aliases for "cd", "pushd" and "popd" so that the
                 updater gets invoked any time the cwd changes.]

   3       #2, plus, "Hey, do you know about the back-quote, or colon,
           (etc) and what you can do with them?"



Some cool add-ons were:

"Strain out your long $HOME path in the cwd and replace it with ~ "
(several authors)
---------------------------------------------------------
As follows:
alias strain_home 'sed "s?$home?~?" '
alias setprompt   'set prompt="`($cwd|strain_home)` % "'
alias cd          'chdir \!*; setprompt'
alias pd          'pushd \!*; setprompt'
alias pop         'popd ; setprompt'
---------------------------------------------------------


"Make certain commands auto-execute on changing to a new directory,
 and on a custom basis"
---------------------------------------------------------
As follows:   (note! line breaks are *arbitrary*, you should manually
               re-join them to make sure it works!)

alias back 'set ts=$cwd; chdir $old; set old=$ts;               \
            if(-r .sme) source .sme; set prompt=$cwd\<\!\>'

alias cd   'set old=$cwd; chdir \!*;                            \
            if( -r .sme ) source .sme;set prompt=$cwd\<\!\>'

... also set the initial prompt in .cshrc:

set prompt=$cwd\<\!\>
---------------------------------------------------------

Rich Baca presented that one, with the following explanation...

|The \<  & \> escape "<" & ">"; they will bracket the command "!"
| in the prompt.  The "if (-r .sme) ..." checks if the new directory
| has an .sme file & sources it if so (ie, reads it as if it were stdin).
|In this way commands can be executed whenever the directory is cd'd to.
| An example .sme:
|
|echo ""
|echo "Includes for XYZ - do not molest!"
|ls -lF


An alternative way of keeping the length of the prompt down
and providing useful information was submitted by Jeffery Ljungstrom.

BTW, he writes:
 | Here's the method I use.  Figuring out how it works is a good exercise
 | in discovering some of the features of csh.  

(thanks, Jeffrey!)


"Strain out all of cwd except the leaf and its parent"
---------------------------------------------------------
as follows:

#
# prompt:
#       
set hn=`hostname`
set user=`whoami`
if ($user == "root") then
        set pchar='#'
else
        set pchar='%'
endif

set cur = `pwd`
set cwdh = $cur:h ; set prompt = "--${hn}:$cwdh:t/$cur:t$pchar "
alias cd \
 'cd \!* ; set cwdh = $cwd:h ; set prompt = "--${hn}:$cwdh:t/$cwd:t$pchar "'

alias 	pushd 			'pushd \!* ; cd .'
alias 	popd 			'popd \!* ; cd .'
---------------------------------------------------------

[The author writes:]
| I've got some extra stuff in there so I can tell if I'm root or not and
| what machine I'm on.  Note that this only shows you the current
| directory and its parent.  If I showed my whole current path on the
| command line I'd be typing off the end of the screen all the time :-)
| Anyway, throw that stuff in your .cshrc and it ought to work as is.


Anyway, that's all I've had time to look at or try.
Some of the above examples are actually combinations of several people's
examples.  I hope no one takes offense, especially those whose fine
examples didn't make it to (this) summary.  As I said before, I've been
very busy (haven't even had time to read the newsgroup! :-( ), so I
haven't tried out all of the stuff that's been mailed to me.

Once again, thanks for all the help.  Maybe we ought to put some of this
stuff in the FAQ, especially including explanations of the functioning
of characters like [`], [:], and [].

Oops.  I can't print that last one on *your* terminal.             ;-)

peter@ficc.ferranti.com (Peter da Silva) (10/02/90)

In article <238@srchtec.UUCP> johnb@srchtec.uucp (John Baldwin) writes:
>    1       "Which shell?"      ...RTNGP, I said "csh prompt."

OK, which CSH? Some have a $cwd variable, and some need an alias. For
those that need an alias, some require the internal cd to be escaped
and some require it to be called "chdir".

RTFQ and explain "RTNGP".
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com