[net.unix] Prompt showing current directory

cim2@pyuxv.UUCP (Robert L. Fair) (11/26/85)

One of the (few) nice things about PC-DOS is that you can set 
your prompt (PS1) to always show your current directory.

The following little script allows you to do the same thing
in the Korn shell ("ksh")

The lines should be put in your '.kshrc' file or equivilent, and
THE ORDER IS SIGNIFICANT.

This works because "ksh" evaluates aliases before builtins/functions.
Note that although the function is called 'ncd' (to avoid conflict
with the built-in 'cd'), the alias allows one to still
do "cd $HOME" etc:

	function ncd {
		alias cd=cd
		cd $1
		PS1="! $PWD >"
		alias cd=ncd
	}
	alias cd=ncd
	PS1=$PWD

Since everthing here is a built-in, the speed is comparable to a regular 'cd'
Anyone know how to get a constant time display in the prompt also ???


Robert L. Fair				
Bell Communications Research/ C.H.C
Piscataway 
N.J.
(pyuxv!cim2)

>>> To Boldly love a maniac linguist, is that not a thing of beauty ????

keith@motel6.UUCP (Keith Packard) (11/28/85)

In article <159@pyuxv.UUCP> you write:

>The following little script allows you to do the same thing
>in the Korn shell ("ksh")
>
>
>	function ncd {
>		alias cd=cd
>		cd $1
>		PS1="! $PWD >"
>		alias cd=ncd
>	}
>	alias cd=ncd
>	PS1=$PWD
>

It's far better to simply enclose cd in single quotes - the alias
will not be expanded so you don't need those other two aliases:

function ncd {
	'cd' $*
	PS1="! $PWD >"
}
alias cd=ncd

keith packard
tektronix!reed!motel6!keith

dcm@busch.UUCP (Craig Miller) (11/29/85)

In article <159@pyuxv.UUCP> cim2@pyuxv.UUCP (Robert L. Fair) writes:
>
>	function ncd {
>		alias cd=cd
>		cd $1
>		PS1="! $PWD >"
>		alias cd=ncd
>	}
>	alias cd=ncd
>	PS1=$PWD

	Another neat thing about ksh is that it checks the PS1 variable
	for $var stuff before it prints it out.  So, the same thing could
	be implemented like this:

		PS1="! \$PWD >"

	(notice that the backslash is needed...)

>Robert L. Fair				
>Bell Communications Research/ C.H.C
>Piscataway 
>N.J.
>(pyuxv!cim2)


-- 
	Craig Miller
	UUCP: ..!ihnp4!we53!busch!dcm
	The Anheuser-Busch Companies; St. Louis, Mo.

- Since I'm a consultant here and not an Anheuser-Busch employee, my
  views (or lack of) are strictly my own.

mts@ms.UUCP (Martin Stanley) (12/02/85)

> One of the (few) nice things about PC-DOS is that you can set 
> your prompt (PS1) to always show your current directory.
> 
> The following little script allows you to do the same thing
> in the Korn shell ("ksh")
> 

Well, here's how I did just that under the csh on a UniPlus+ port
(System Vr1). It is obviously slower than under BSD since the cwd
variable there is maintained by the shell.


set CWD = `pwd`
set prompt="($CWD) "
  
alias cd chdir \!\* \; set CWD=\`/fast/pwd\` \; set prompt = \"\(\$CWD\) \"


While we are at it, I also have a simulation of pushd and popd (from the
BSD4.2 csh). They are implemented via aliases and shell files. They too,
are considerably slower than the builtin versions, but work fast
enough.  Send me mail if you want a copy.
-- 

 Martin Stanley
 Department of Computer Science
 University of Toronto
 Toronto, ON
 M5S 1A4

 USENET:	{decvax,ihnp4,linus,uw-beaver}!utcsri!utai!ms!mts
 CSNET:		mts@toronto
 ARPANET:	mts.toronto@csnet-relay

ref0070@ritcv.UUCP (Bob Fortin) (12/10/85)

To have your prompt showing the current directory in C-shell use:
      alias cd `cd \!*; set prompt=$cwd:t`