[comp.unix.shell] Current directory in the ksh prompt?

mju@mudos.ann-arbor.mi.us (Marc Unangst) (04/28/91)

I realize this is probably a FAQ, but I can't seem to find the FAQ
list on this machine or any of the NNTP servers around here (if you
know of an NNTP server that accepts connections from anybody and keeps
news a long time, like for over two weeks, please let me know!), so
I'll ask away...

I recently started using ksh, and one thing I miss dearly from csh is
the ability to put the current directory in your prompt.  With csh, it
can be done by aliasing the cd command to something that does the cd
and then re-sets your prompt, using `pwd` to get the new directory.
Unfortunately, it seems that you can't use the parameters inside an
alias like you can in csh, so this trick doesn't work.  Is there a
different trick for ksh, or am I just going to have to live with
typing "pwd" all the time?

--
Marc Unangst               |
mju@mudos.ann-arbor.mi.us  | "Bus error: passengers dumped"
...!hela!mudos!mju         | 

mju@mudos.ann-arbor.mi.us (Marc Unangst) (04/28/91)

I wrote:
> I recently started using ksh, and one thing I miss dearly from csh is
> the ability to put the current directory in your prompt.  With csh, it
[...]

Well, I've gotten many replies to this question.  The general idea is
to create a function called "_cd" (or something similar), which does
the actual cd and then resets the prompt, and then alias cd to this
function.  The slickest answer was by Mike O'Connor, who submitted the
following function:

function _cd {
	unalias cd
	cd $1
	alias cd=_cd
	if [ "${PWD#${HOME}}" != "${PWD}" ]
	then
		PS1='"${USER}@${HOSTNAME%%.*}:~/${PWD#${HOME}}> "'
	else
		PS1='"${USER}@${HOSTNAME%%.*}:${PWD}> "'
	fi
}

This cleverly uses the ksh string-substitution features to strip the
trailing domain name off $HOSTNAME, as well as substitute "~/" for the
leading path when the current directory is in or below your home
directory.  The "unalias cd; cd $1; alias cd=_cd" part could be
replaced with "\cd $1", I believe; otherwise, this answer is almost
perfect.

Thanks to everybody who replied to this!

--
Marc Unangst               |
mju@mudos.ann-arbor.mi.us  | "Bus error: passengers dumped"
...!hela!mudos!mju         | 

mike@bria.UUCP (Michael Stefanik) (04/28/91)

In an article, mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:
>I recently started using ksh, and one thing I miss dearly from csh is
>the ability to put the current directory in your prompt. [...]

My personal favorite is this one (it prints out the basename of the
current directory):

PS1='${PWD##*/}> '

If you want the whole thing, then just use some variation of:

PS1='${PWD}> '

-- 
Michael Stefanik, MGI Inc, Los Angeles | Opinions stated are never realistic
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
If MS-DOS didn't exist, who would UNIX programmers have to make fun of?

logier@cheops.qld.tne.oz.au (Rob Logie) (04/29/91)

mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:

>I realize this is probably a FAQ, but I can't seem to find the FAQ
>list on this machine or any of the NNTP servers around here (if you
>know of an NNTP server that accepts connections from anybody and keeps
>news a long time, like for over two weeks, please let me know!), so
>I'll ask away...

>I recently started using ksh, and one thing I miss dearly from csh is
>the ability to put the current directory in your prompt.  With csh, it
>can be done by aliasing the cd command to something that does the cd
>and then re-sets your prompt, using `pwd` to get the new directory.
>Unfortunately, it seems that you can't use the parameters inside an
>alias like you can in csh, so this trick doesn't work.  Is there a
>different trick for ksh, or am I just going to have to live with
>typing "pwd" all the time?

>--
>Marc Unangst               |
>mju@mudos.ann-arbor.mi.us  | "Bus error: passengers dumped"
>...!hela!mudos!mju         | 



Try

	PS1="`id -un`@`hostname`:"'$PWD'" $ "; export PS1

You will have to customise the id and hostname commands to what ever you
machine uses to return current user id and hostname.



-- 
Rob Logie                                    EMAIL: logier@cheops.qld.tne.oz.au
Telecom Australia                            FAX:   +61 7 837 4704
TNE Computer Support Services                PH:    +61 7 837 5174
Brisbane Office                              "These are my opinions alone"

odin@jomby.cs.wisc.edu (Odin Anderson) (04/30/91)

In article <kLF411w164w@mudos.ann-arbor.mi.us> mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:
>I wrote:
>> I recently started using ksh, and one thing I miss dearly from csh is
>> the ability to put the current directory in your prompt.  With csh, it
>[...]
>
>Well, I've gotten many replies to this question.  The general idea is
>to create a function called "_cd" (or something similar), which does
>the actual cd and then resets the prompt, and then alias cd to this
>function. 

Isn't the folowing more simple while still doing what you want:
	 PS1=" \$PWD> "
This works well for me (I like the whole path).  

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Sincerely,       + Programing is like _______, it must be _______ to be good.+
+     __             + What is the most stable 3-dimensional shape?            +
+    /  ) __/ . __     + How many bytes are in a mouthfull?                    +
+   (__/ (_<_<_/ <_      + Never, Never, Say never...                          +
+                          + Always carry a fingernail clipper!                +
+   odin@jomby.cs.wisc.edu   + Don't ask questions you already know.           +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

dfpedro@uswnvg.UUCP (Donn Pedro) (04/30/91)

In article <1991Apr30.021429.2072@spool.cs.wisc.edu>, odin@jomby.cs.wisc.edu (Odin Anderson) writes:
> Isn't the folowing more simple while still doing what you want:
> 	 PS1=" \$PWD> "
> This works well for me (I like the whole path).  

If you only want the last component of the path:

	PS1='${PWD##*/} > '

Note that this is running under ksh.


	dfpedro@uswnvg.UUCP

sam@ms.uky.edu (Mike Mills) (04/30/91)

mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:

>I wrote:
>> I recently started using ksh, and one thing I miss dearly from csh is
>> the ability to put the current directory in your prompt.  With csh, it
>[...]

Rather than a function, you can just do this:

PS1='${PWD##*/}<!>$ '

...this gives you the PWD with current history #, and a $ prompt...


-- 
--Mike Mills              E-Mail:  sam@ms.uky.edu, {rutgers, uunet}!ukma!sam
--University of Kentucky                  mike@ukpr.uky.edu    
--(606) 255-3583, 257-3092 
    "Drawing on my fine command of language, I said nothing." -- Mark Twain

alex@am.sublink.org (Alex Martelli) (05/05/91)

mju@mudos.ann-arbor.mi.us (Marc Unangst) writes:
	...
:directory.  The "unalias cd; cd $1; alias cd=_cd" part could be
:replaced with "\cd $1", I believe; otherwise, this answer is almost
:perfect.

One little tidbit to push it 0.0001 nearer to perfection: change that
$1 to $*, so you don't lose the ksh feature to say, e.g., 'cd lib spool'
to change from /usr/lib/uucp to /usr/spool/uucp...
-- 
Alex Martelli - (home snailmail:) v. Barontini 27, 40138 Bologna, ITALIA
Email: (work:) martelli@cadlab.sublink.org, (home:) alex@am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; 
Fax: ++39 (51) 366964 (work only), Fidonet: 332/401.3 (home only).