How To: set PS1 to show users "current" directory?

Alex Martelli martelli at cadlab.sublink.ORG
Sat Apr 27 02:51:14 AEST 1991


rol at grasp1.univ-lyon1.fr (Paul Rolland) writes:
	...
:   That works fine, but I'd like to have the working directory relative
:to my HOME directory. That's is : my HOME is /u/rol. If I'm in /u/rol/tmp/c,
:I'd like to have the prompt being tmp/c, or even better, HOME/tmp/c when I'm
:in /u/rol/tmp/c and /u when I'm in /u...

This is my approach in ksh (in the file that's in envir. var ENV):
---start cut:
tradir()
{
	echo $* | sed -e 's,^'$HOME',~,' -f $HOME/.dirsed
}
dirnam()
{
	if [ -r .dirnam ]
	then cat .dirnam
	else tradir $PWD
	fi
}
setpro()
{
	export PS1="${LOGNAME}_$THOST `dirnam` [!] "
}
mycd()
{
	'cd' $*; setpro
}
alias cd=mycd
---end cut
and in $HOME/.dirsed, I have:
---start cut
s,^/\([a-z0-9]*\)root/,\1:,
---end cut
That's a bit elaborate, but I like it!  If a directory contains a file named
.dirnam, then the contents of that file are to be displayed as the name of
the directory; else, a leading instance of the user's home directory, if any,
is translated to tilde, then sed is passed a user's personalized editing
script for directories-in-prompt, which in my case just translates 'root' 
into ':' if the leading directory is a child of / and its name ends with
'root' (that's because we nfs-mount pippo:/ as /pipporoot...).
setpro is a separate shell function because it can be used not only after
a cd but also after a push-directory, pop-directory, etc.
I'm not using the ksh's option to just expand a variable such as ${PWD} in
the prompt, because I want to do (potentially) rather sophisticated editing,
which can be a bit costly, and I want to pay that cost only at a cd rather
than over and over again!
-- 
Alex Martelli - CAD.LAB s.p.a., v. Stalingrado 53, Bologna, Italia
Email: (work:) martelli at cadlab.sublink.org, (home:) alex at am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; 
Fax: ++39 (51) 366964 (work only), Fidonet: 332/401.3 (home only).



More information about the Comp.unix.aix mailing list