[comp.unix.questions] Pathname in prompt, not FAQ, really.

avalon@tardis.computer-science.edinburgh.ac.uk ("Scott A.C. McIntyre") (06/12/90)

Newsgroups: comp.unix.questions
In-Reply-To: 
Organization: Time And Relative Dimensions in Space, plc.
Cc: 


Im interested in finding out how I can get a Sys V type machine to 
include my cwd in my prompt...Currently, what it does is what was
suggested by the FAQ sheet that someone sent out a while ago, however,
I was wondering if there was a better way..

Namely, when I am in my home directory I want it to display a ~ 
instead of the path of my whole directory...secondly, I would like a
prompt which when I hit cd and return would send me $home and change
my prompt back to ~

Once upon a time in tcsh in BSD 4.3 I was able to use dirs in the prompt
command...whenever I do that here, it ALWAYS returns a ~, which doesn't
really help much...

And trying to just cd to get home gives me a no such file error...

Suggestions?  

Thanks,
Scott

-- 
avalon@exeter.ac.uk
	avalon@tardis.cs.ed.ac.uk
		avalon@ssyx.ucsc.edu
			avalon@ucscb.ucsc.edu

meissner@osf.org (Michael Meissner) (06/12/90)

In article <sent.Tue.Jun.12.09:57:20.Lon.1990.via.CS.TARDIS>
avalon@tardis.computer-science.edinburgh.ac.uk ("Scott A.C. McIntyre")
writes:

| Im interested in finding out how I can get a Sys V type machine to 
| include my cwd in my prompt...Currently, what it does is what was
| suggested by the FAQ sheet that someone sent out a while ago, however,
| I was wondering if there was a better way..
| 
| Namely, when I am in my home directory I want it to display a ~ 
| instead of the path of my whole directory...secondly, I would like a
| prompt which when I hit cd and return would send me $home and change
| my prompt back to ~
| 
| Once upon a time in tcsh in BSD 4.3 I was able to use dirs in the prompt
| command...whenever I do that here, it ALWAYS returns a ~, which doesn't
| really help much...
| 
| And trying to just cd to get home gives me a no such file error...
| 
| Suggestions?  

Yes, if you are willing to switch shells.  The GNU Bourne Again Shell
has this feature.  For example, if you do:

	PS1='[\h]% '
	export PS1

it will display:

	[~]% 

if you are in your home directory,

	[~/subdir]%

if you in the subdirectory 'subdir' in your home directory, and
finally:

	[/tmp]%

if you are in the dictory /tmp.

In addition, both bash & ksh allows you to run arbitrary commands in
your prompt, via the $(command) syntax.  For example, I use something
similar to the following to give me the amount of free space in the
disk of the current directory.  The program free-space is a simple
hack I wrote to get the free space, and canonicalize the directory.
The '\' before the ${PWD} is needed for ksh, but not for bash.  For
bash, if you have a multiline prompt, like I use, you will need to
make a small fix to get the command line editing stuff to not mess
things up.

PS1='$(free-space -c \${PWD})
--> '
export PS1
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA

Catproof is an oxymoron, Childproof is nearly so

montnaro@spyder.crd.ge.com (Skip Montanaro) (06/13/90)

I alias cd to the following ksh function to put the current directory in the
title bar of my xterms. It does the ~ substitution as requested. Note that
there are two ASCII ESC chars ("^[") and two ASCII BEL chars ("^G") in the
code below. Substitute (Saturate? :) before using, folks.

    hostname=$(hostname)
    function cdxterm {
	\cd "$@"
	dir="${PWD#$HOME/}"
	if [ $dir != $PWD ] ; then
	    dir="~/$dir"
	elif [ $dir = $HOME ] ; then
	    dir="~"
	fi
	print -n "^[]1;$hostname^G^[]2;$hostname : $dir^G"
    }

--
Skip (montanaro@crdgw1.ge.com)

tif@doorstop.austin.ibm.com (/32767) (06/13/90)

In article <MEISSNER.90Jun12111358@curley.osf.org> meissner@osf.org (Michael Meissner) writes:
>In addition, both bash & ksh allows you to run arbitrary commands in
>your prompt, via the $(command) syntax.

Really?  I have been unsuccessful in doing this with ksh.
Please demonstrate.

Paul Chamberlain | I do NOT represent IBM	  tif@doorstop, sc30661@ausvm6
512/838-7008	 | ...!cs.utexas.edu!ibmaus!auschs!doorstop.austin.ibm.com!tif

khan@THOR.XRAYLITH.WISC.EDU (Mumit Khan) (06/13/90)

::Namely, when I am in my home directory I want it to display a ~ 
::instead of the path of my whole directory...secondly, I would like a
::prompt which when I hit cd and return would send me $home and change
::my prompt back to ~

I usually do the following on all our machines that doesn't have TCSH,
and it works very nicely. I alias CD to the following:

alias cd 'cd \!*; source ~/.prompt'

where .prompt contains the following:
    
#! /bin/csh -f
set prompt="${USER}@${HOST} `pwd | sed 's?'${HOME}'?~?'`:\!>"

works fine for me. I don't remember what was in the FAQ sheet, so
please ignore if this has already been proposed. Having tcsh certainly
helps, but this works ok for me on our AIX and SYSV machines.

Mumit -- khan@xraylith.wisc.edu