[comp.unix.wizards] How to have PS1 evaluated after each "cd"

lee@netsys.UUCP (Lee Chen) (12/31/87)

  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 ?

  Thanks in advance to those who care to respond.

    \\Lee

{ihnp4|decuac|ames}!netsys!lee

karthur@codas.att.com (Kurt_R_Arthur) (12/31/87)

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 ?
> 
In ksh I have set the folllowing in my .profile to set the path as part of
the prompt:

PS1="`uname`
\${PWD}>"

This makes my prompt the machine name, a new line, and the path followed by
a ">".  Example:

			codas
			/usr/karthur>

If you don't want the machine name in the prompt, just:
PS1="\${PWD}>"

The \ is required as an escape for the $.  Hope this helps!


Kurt Arthur
Software Services of Florida, Inc.

emb978@leah.Albany.Edu ( Eric M. Boehm) (01/01/88)

	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 ?

I use the following in .cshrc to set current working directory in the
prompt.

alias cd 'set oldwd=$cwd; chdir \!*; set prompt="\! <$cwd>" '

set prompt=\! <$cwd>" is also in .login to set initial prompt.

Eric M. Boehm
EMB978@ALBNY1VX.BITNET
EMB978@LEAH.ALBANY.EDU

eap@bucsb.UUCP (Eric Pearce) (01/01/88)

In article <1876@netsys.UUCP> lee@netsys.UUCP (Lee Chen,Staff) 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 ?

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

mlandau@bbn.com (Matt Landau) (01/01/88)

In comp.unix.wizards (<1374@bucsb.UUCP>), eap@bucsb.UUCP (Eric Pearce) writes:
>I've been using this for quite a while under csh/tcsh (I did not write
>it myself)
>
> [Demo of using alias to change prompt on cd, pushd, etc.]

Well, if you can run tcsh, you can use the precmd alias to have your prompt
set each time it is printed.  I use to following on my Suns:

    if ($TERM == "sun" && `tty` != "/dev/console" && ! $?EMACSEDIT_TTY) then
      set   prompt = "%S$prompt%s"
      alias precmd  echo -n '^[]l  ${root}${HOST}:${cwd}^[\\ '
    else if ($TERM =~ xterm* && ! $?EMACSEDIT_TTY) then
      alias precmd  echo -n '^[]l  ${root}${HOST}:${cwd}^[ '
    endif

$HOST and $cwd are maintained by tcsh, and $root is set by a simple test 
to "#" if I have superuser privileges in this shell.  The escape sequences
set the window banner for each shelltool or xterm window so that it contains
the hostname and working directory, in a form like "diamond:/usr/local/src".

Since this precmd uses only shell variables and builtins, it's acutally fast 
enough that you can reset the prompt after every command without feeling any 
performance penalty.
--
 Matt Landau			Waiting for a flash of enlightenment
 mlandau@bbn.com			  in all this blood and thunder

kent@tifsie.UUCP (Russell Kent) (01/05/88)

( #include <references> :-)

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

marki@hpiacla.HP.COM (Mark Ikemoto) (01/08/88)

Russell wrote up that nice summary and, of course, someone like me has
to come along and offer his/her two cent's worth.  Here it is...

One annoyance to me in having the current working directory in
the prompt is that depending on how long the working directory
pathname is in the prompt, the starting point for my command
could be in any position on the screen.  If you changed directories
often or hate your commands to hit the right side of the screen or
don't like zig-zagging your eyes across a screen looking for the
format of a command previously entered, you may want to think about
implementing a two-line prompt.

I alias cd to a set of commands that sets up my show-current-directory
prompt.  Within this set, I also insert terminal-specific control
characters that manipulate the cursor to display a two-line prompt.
So when I cd anywhere, I get a blank line followed by the current
working directory pathname on the next line followed by a line
containing my system's node name, my user name, and C-shell history
number, e.g., 

CWD: /usr/spool/lp/interface
ZEIT:mark:[34]:    ...your command goes here...

If you have access to a color terminal, it's even better since you
can also add terminal control chars, for example, to make your CWD
line a different color from your command line.  In any case, your
command always starts at the same character position on the line no
matter how long the working directory pathname.


Mark
 

emb978@leah.UUCP (01/12/88)

In article <4070008@hpiacla.HP.COM>, marki@hpiacla.HP.COM (Mark Ikemoto) writes:
> I alias cd to a set of commands that sets up my show-current-directory
> characters that manipulate the cursor to display a two-line prompt.
> So when I cd anywhere, I get a blank line followed by the current


I tried something along the lines of set prompt="\! $cwd>\n" but I got the
\n as part of the prompt. How does one insert the linefeed?

Eric M. Boehm
EMB978@LEAH.ALBANY.EDU
EMB978@ALBNY1VX.BITNET