Path: utzoo!mnetor!uunet!husc6!hao!gatech!mcnc!rutgers!mtune!codas!killer!pollux!ti-csl!tifsie!kent From: kent@tifsie.UUCP (Russell Kent) Newsgroups: comp.unix.wizards Subject: Re: How to have PS1 evaluated after each "cd" Message-ID: <278@tifsie.UUCP> Date: 5 Jan 88 01:16:56 GMT References: <1876@netsys.UUCP>,<1374@bucsb.UUCP>,<551@leah.Albany.Edu> Organization: TI Process Automation Center, Dallas Texas Lines: 114 ( #include :-) In article <1876@netsys.UUCP> lee@netsys.UUCP (Lee Chen) writes: > Not So Long Ago there was a discussion about how to have the present >working directory evaluated and displayed as part of the PS1 prompt. >Would anyone (or two :-) care to summarize on effective solutions to >this "problem" via ksh, csh, sh, *sh script or C ? Basically, there are two approaches to this problem: 1. Evaluate the current working directory prior to prompting the user, and re-create the prompt with the current directory. 2. Modify the behavior of the various directory-changing commands (typically cd, pushd, and popd) so that they re-create the prompt when the directory changes. Frequently, 1. requires an unacceptable amount of CPU time to be spent after commands which do not change the directory (why re-evaluate the prompt after a "vi" or "make" command??). Of course, it is also aesthetically more pleasing :-) to have the prompt re-evaluated only when it needs to be changed (yes I know this is vague). In article <551@leah.Albany.Edu> emb978@leah.Albany.Edu ( Eric M. Boehm) says: > I use the following in .cshrc to set current working directory in the > prompt. > > alias cd 'set oldwd=$cwd; chdir \!*; set prompt="\! <$cwd>" ' > Eric M. Boehm This works fairly well, and avoids the distasteful waste of CPU cycles to re-evaluate the prompt superfluously. It also takes care to pass all of the command parameters to the chdir command. (That way chdir can moan about "chdir dir-with-wildcard" ambiguities). However, it fails to take advantage of a neat little trick (or at least Eric failed to mention it): you can create an alias ("back") which will throw you back to the directory you came from: alias back 'cd $oldwd' Of course, now we have the problem of what happens to $oldwd when we try to chdir to a non-existant directory (haven't you *ever* mis-typed a directory name?? :-) : it gets clobbered, so that it is not possible to go "back." The solution is of course to make the assigment of the "oldwd" variable conditional on the success of the chdir. But if the chdir succeeds, then "$cwd" won't be what we want anymore. Time for a new variable. alias cd '\ set back=$cwd; chdir \!* && set oldwd=$back; set prompt="\! <$cwd>"' alias back 'cd $oldwd' in article <1374@bucsb.UUCP>, eap@bucsb.UUCP (Eric Pearce) says: > I've been using this for quite a while under csh/tcsh (I did not write > it myself) > > # the following will set the prompt for the user to be the last element > # of the current directory path. To remove this remove the ":t" from the > # setprompt alias, the cd, pushd, popd aliases work to maintain the correct > # directory printed on the screen > alias setprompt 'set prompt="$cwd:t ";\\ > if (x$cwd == x"/")set prompt="[/] ";' > alias cd 'if ("\!:0-$" == cd) cd;\\ > if ("\!:0-$" != cd) cd \!$;\\ > setprompt' > alias pushd 'if ("\!:0-$" == pushd) pushd;\\ > if ("\!:0-$" != pushd) pushd \!$;\\ > setprompt' > alias popd 'if ("\!:0-$" == popd) popd;\\ > if ("\!:0-$" != popd) popd \!$;\\ > setprompt' > setprompt; > UUCP !harvard!bu-cs!bucsb!eap ARPANET eap@bucsb.bu.edu CSNET eap%bucsb@bu-cs Oy vay! I can only begin to understand what all that stuff above is about. The setprompt is staright-forward enough: form the prompt. If we're at the root filesystem (in which case the :t "tail" of the path is null), then substitute the "[/]." The "cd" alias appears truely demented. Appearantly, if the entire command is just "cd", then just "cd", otherwise "cd" to the last "word" on the command line. This means that: cd /etc /usr /usr0 /usr1 /usr2 /tmp is an acceptable command. This is obviously not in line with the existing definition ("performance") of the cd command. (OK OK, I know no half-way sane person would give such a command, but who among you has *never* accidentally give more than one parameter to a cd command??) Also, the alias goes to needless trouble to determine whether or not it has parameters: use the \!* to pick up all the parameters (if any) and give them to the "cd" command: alias cd 'cd \!*; setprompt' The same problems afflict the "pushd" and "popd" aliases (Of course, I am at a loss as to why this alias and the original alias don't go into a recursive macro expansion. Eric's aliases don't work on *my* tcsh 5.4 (Ohio State) 7/18/87 Patch level 0 running on a MicroVAX Ultrix 2.1 system) Please gentle readers, do not misconstrue my comments and criticisms as an attack on either of the replies cited here, or on their authors. I'm not sure whose opinions these are: I found them on the sidewalk. The owner(s) may claim them at the following address: Russell Kent Phone: +1 214 995 3501 Texas Instruments - MS 3635 Net mail: P.O. Box 655012 ...!{ihnp4,uiucdcs}!convex!smu!tifsie!kent Dallas, TX 75265 ...!ut-sally!im4u!ti-csl!tifsie!kent -- Russell Kent Phone: +1 214 995 3501 Texas Instruments - MS 3635 Net mail: P.O. Box 655012 ...!{ihnp4,uiucdcs}!convex!smu!tifsie!kent Dallas, TX 75265 ...!ut-sally!im4u!ti-csl!tifsie!kent