[comp.unix.questions] Getting UNIX prompt to display curr

mcdonald@uxe.cso.uiuc.edu (03/21/89)

>In MS-DOS there is a very easy way to get the current directory displayed as
>part of the prompt  (prompt $p).  In UNIX, there is certainly no
>straightforward way to do this.  Can anyone think of a tricky way?  

Maybe the Unix.Gods should put it in in a simple way and give it
a prominent place in TFM.

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) (03/22/89)

Here are a couple of sicko aliases that set the prompt to the current
directory.  The first:

alias cd cd \!\*\; set prompt = \
  pwd \| awk \'BEGIN \{FS=\"/\"\} \{print \$\'NF\'\}\'\`\"\> \"

causes "cd" to change directory and set the prompt to "thisdir> ",
where "thisdir" is the name of the current directory only, *not* its full
pathname.

The second:

alias asdf cd \!\*\; set prmpt = \`pwd \| awk \'BEGIN \{FS=\"/\"\} \
       \{cnt = NF\; cnt -- \; \
        printf \"\%-1s/\%-1s\>\", \$\'cnt\', \$\'NF\' \}\'\` \; \
        set prompt = \"\$prmpt \"

 causes "cd" to change directory and set the prompt to
"parentdir/thisdir> ", where "parentdir" is the name of the parent
directory of the current directory.

These definitions are mildly machine-dependent.  As they are, they both
work in Ultrix.  When I tried them on an Ardent Titan running System V,
I had to add a semicolon after the BEGIN{...} statements.
(If you are really going to make the change, you should 
insert a \; after the closing brace of each BEGIN{...} statement.
 
P.S.1:  I do not use these aliases myself -- I would rather stick to
a fixed, well-chosen, pre-defined prompt and do 'pwd' when I forget where
I am in the directory tree.

P.S.2:  Someone posted a solution recently for making the prompt the 
*full* pathname of the current directory.  The solutions above
were inspired by that posting.

P.S.3:  To those people who continue flaming about frequent and repeated 
postings of elementary questions in this news group:  Short of starting
a comp.unix.questions.elementary, if the answer to the 
question is really short and simple, please just post it and let it
go at that.  More boring than silly questions are flames about silly 
questions. 

-- 
Rouben Rostamian
Department of Mathematics                      e-mail:
University of Maryland Baltimore Counnty       Rostamian@umbc2.bitnet
Baltimore, MD 21228                            rostamia@umbc3.umbc.edu

lbn@ksuvax1.cis.ksu.edu (Lars Bo Nielsen) (03/25/89)

Simply (in CSH):

1) Full pathname:
	alias cd 'cd \!*; set foo=`pwd`; set prompt=$foo"% "'
2) Only name of current dir:
	alias cd 'cd \!*; set foo=`pwd`; set prompt=$foo:t"% "'
3) Include name of machine in prompt
	alias cd 'cd \!*; set foo=`pwd`; set prompt=$host":"$foo"% "'
or	alias cd 'cd \!*; set foo=`pwd`; set prompt=$host":"$foo:t"% "'
--
 Lars Bo Nielsen          || BITNET: lbn@KSUVAX1
 lbn@ksuvax1.cis.ksu.edu  || UUCP: ...!{rutgers,texbell,atanasoff}!ksuvax1!lbn

jms@hcx.uucp (Michael Stanley) (03/25/89)

In article <1383@deimos.cis.ksu.edu>, lbn@ksuvax1.cis.ksu.edu (Lars Bo Nielsen) writes:
> Simply (in CSH):
> 
> 1) Full pathname:
> 	alias cd 'cd \!*; set foo=`pwd`; set prompt=$foo"% "'
> 2) Only name of current dir:
> 	alias cd 'cd \!*; set foo=`pwd`; set prompt=$foo:t"% "'
> 3) Include name of machine in prompt
> 	alias cd 'cd \!*; set foo=`pwd`; set prompt=$host":"$foo"% "'
> or	alias cd 'cd \!*; set foo=`pwd`; set prompt=$host":"$foo:t"% "'

Ah, but csh includes the environment variable $cwd.  This makes setting
foo unnecessary.  It seems to me that you could remove the part that says:

	set foo=`pwd`

and replace every occurrence of $foo with $cwd.

I haven't actually tested these examples so I hope I didn't just stick
my foot in my mouth, but I did do something like this once so I'm pretty
sure it'll work.  I only make this suggestion because if your system is
running slow, every extra binary you run (in this case 'pwd') will just
be that much more irritation.


	Michael Stanley      (...!uunet!harris.cis.ksu.edu!jms@hcx)

tale@its.rpi.edu (David C Lawrence) (03/25/89)

In article <2113@cveg.uucp> jms@hcx.uucp (Michael Stanley) writes:
MS> Ah, but csh includes the environment variable $cwd.  This makes setting
MS> foo unnecessary.  It seems to me that you could remove the part that says:

MS> 	set foo=`pwd`

MS> and replace every occurrence of $foo with $cwd.

MS> I haven't actually tested these examples so I hope I didn't just stick
MS> my foot in my mouth, but I did do something like this once so I'm pretty
MS> sure it'll work.

Well, you didn't necessarily stick your foot in your mouth.  $cwd
works just fine for people who don't care where they REALLY are in
the directory tree (ie, those who never bother with .. ).  However,
for those of us who do use .. as a convenient shorthand way to move
around directories, $cwd doesn't cut it.  If the directory to which
you changed was in reality a symbolic link, $cwd (and dirs) reports
the name of the link, not the final destination.  Using pwd for the
prompt insures that the actual current directory will be displayed.

For anyone who is really interested in the 80 000 ways that other
people are setting their prompt, here's my obligatory contribution.
Perhaps the original fellow who asked the question (with the Bourne
shell) would like to get tcsh -- prompt-people can play to their
hearts delight in it.  The following are in tcsh:

alias precmd pwd
set prompt="%m:`whoami` (%h) "

to give me:

/home/h/tale/News
consult1:tale (87) 

tcsh also has an available variable "cwdcmd" to be executed whenever
the current value of cwd changes.  This can be set to change the
window label of a shelltool, for example, by aliasing it to 

alias cwdcmd echo ^[]l$cwd^[\\

For those interested, tcsh is available from tut.cis.ohio-state.edu
via anonymous ftp.

Dave
--
      tale@rpitsmts.bitnet, tale%mts@itsgw.rpi.edu, tale@pawl.rpi.edu

lbn@ksuvax1.cis.ksu.edu (Lars Bo Nielsen) (03/27/89)

In article <2113@cveg.uucp> jms@hcx.uucp (Michael Stanley) writes:
>
>In article <1383@deimos.cis.ksu.edu>, lbn@ksuvax1.cis.ksu.edu (Lars Bo Nielsen) writes:
>> Simply (in CSH):
>> 
>> 1) Full pathname:
>> 	alias cd 'cd \!*; set foo=`pwd`; set prompt=$foo"% "'
>>  [ Three more examples ]
>
>Ah, but csh includes the environment variable $cwd.  This makes setting
>foo unnecessary.  It seems to me that you could remove the part that says:
>
>	set foo=`pwd`
>
>and replace every occurrence of $foo with $cwd.

You are right. Here are the modified (quicker) versions:

1) Full pathname:
	alias cd 'cd \!*; set prompt=$cwd"% "'
2) Only name of current dir:
	alias cd 'cd \!*; set foo=$cwd; set prompt=$foo:t"% "'
3) Include name of machine in prompt
	alias cd 'cd \!*; set prompt=$host":"$cwd"% "'
or	alias cd 'cd \!*; set foo=$cwd; set prompt=$host":"$foo:t"% "'

>	Michael Stanley      (...!uunet!harris.cis.ksu.edu!jms@hcx)
--
 Lars Bo Nielsen          || BITNET: lbn@KSUVAX1
 lbn@ksuvax1.cis.ksu.edu  || UUCP: ...!{rutgers,texbell,atanasoff}!ksuvax1!lbn