[comp.unix.wizards] prompt changing on the fly

wyle@ethz.UUCP (01/16/87)

Does anyone out there have a shell script / program to change
the prompt DYNAMICALLY after each [CR]?

I would like to choose a random prompt from a large file, and
I would like the flexibility to add: node, directory, etc. to
the generated prompt.

I would prefer NOT to write a new shell.

Ideas?
---

-- 
                      M i t c h e l l     F     W y l e

    EEEEE TTTTTT H   H  Eidgenoessische      | wyle%ifi.ethz.chunet@relay.cs.net
   E       T    H   H  Technische Hochschule | wyle@ethz.uucp
  EEEE    T    HHHHH  Zuerich                | ...!cernvax!ethz!wyle
 E       T    H   H  Institut fuer Informatik| Telephone: 011 41 1 256 5235 
EEEEE   T    H   H  8092 Zuerich, Switzerland| "Ignore alien orders"

lvc@danews.UUCP (01/18/87)

What shell are you using?  It is possible in the Korn shell.
Impossible in the Bourne shell.  I don't know about the C-shell.
My prompt includes a clock that is updated at every keystroke.
-- 

Larry Cipriani   Cornet 353-4999 AT&T (614) 860-4999
{ihnp4|cbosgd}!cbsck!lvc   AT&T Network Systems rm 2B-220

cdash@boulder.UUCP (Charles Shub) (01/20/87)

In article <432@ethz.UUCP> wyle@ethz.UUCP (Mitchell Wyle) writes:
>Does anyone out there have a shell script / program to change
>the prompt DYNAMICALLY after each [CR]?
>
it is simple to define a command/alias to set your prompt and then redefine
commands to also set your prompt

alias cd 'chdir \!*;SP'
alias SP 'set prompt="$MACHINE-`basename $cwd` \! > "'

then the prompt does what you want

john@moncskermit.oz (John Carey) (01/21/87)

Here is an alias for csh which replaces cd to change the prompt
when moving directories

The only problem I have found is that it must have an argument

	alias nd cd \!\$\;set prompt=\`pwd\`\' \'

I hope some else can suggest a more elegant solution for csh

						John Carey.

						john%monu1.oz@seismo.ARPA

geoff@ncr-sd.UUCP (Geoff Walton ) (01/22/87)

Due to the facts that I have accounts on four (five?) different
machines, and often forget what directory (let alone what
machine) I'm in, I find the following aliases helpful:

alias cd    'cd \!* ; set prompt="[1m\\!:`uuname -l`:[$cwd][0m "'
alias popd	'popd ; set prompt="[1m\\!:`uuname -l`:[$cwd][0m "'
alias pushd 'pushd \!* ; set prompt="[1m\\!:`uuname -l`:[$cwd][0m "'

You might want to remove the escape sequences ([1m and [0m),
as the do nothing but highlight the prompt on my vt 100, but the
resulting prompt gives you:

history#:machine_name:[current_directory]

Ain't no big deal, but it helps.

:r all_usual_disclaimers

Geoff Walton
NCR E&M San Diego
{wherever}!ucbvax!sdcsvax!ncr-sd!geoff
Even the smallest problem becomes unsolvable if enough meetings
are held to discuss it.

terryl@tekcrl.UUCP (01/22/87)

In article <410@moncskermit.oz> john@moncskermit.oz (John Carey) writes:
>Here is an alias for csh which replaces cd to change the prompt
>when moving directories
>
>The only problem I have found is that it must have an argument
>
>	alias nd cd \!\$\;set prompt=\`pwd\`\' \'
>
>I hope some else can suggest a more elegant solution for csh


     Try this:


	alias cd 'cd \!*; set prompt=$cwd> '

     The single quotes are important (so's $cwd doesn't get interpreted when
the alias is read, but when it's executed), and the \!* gets around John's
problem about always having to have an argument. Just for completeness, try
these aliases also:

	alias pushd 'pushd \!*; set prompt=$cwd> '
	alias popd 'popd; set prompt=$cwd> '

chris@mimsy.UUCP (Chris Torek) (01/22/87)

(There is one good thing about the snow that shut down D.C. so that
 I am missing all the Usenix talks.  At least I get to catch up on
 news!  :-) )

In article <410@moncskermit.oz> john@moncskermit.oz (John Carey) writes:
>Here is an alias for csh which replaces cd to change the prompt
>when moving directories
>
>The only problem I have found is that it must have an argument

(The cure for this is to use !*.)

>	alias nd cd \!\$\;set prompt=\`pwd\`\' \'

Here is one I have used in the past.  It preserves the C shell's
tilde notation.

	alias set_prompt 'set prompt="[\\!] `dirs|awk '\''{print "\$1"}'\''` "'
	alias cd 'cd \!*; set_prompt'
	alias pushd 'pushd \!*; set_prompt'
	alias popd 'popd \!*; set_prompt'
	set_prompt
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu

storm@diku.UUCP (01/23/87)

In article <432@ethz.UUCP> wyle@ethz.UUCP (Mitchell Wyle) writes:
>Does anyone out there have a shell script / program to change
>the prompt DYNAMICALLY after each [CR]?

You cannot use a shell script or a program to change the shell prompt
dynamically, because the PS1/prompt variables are local to the shell,
and therefore they can only be changed by the shell itself.  

I don't know of any hooks into the Bourne/C shells which enables you to
recognize that CR has been typed, and thus not to execute any commands
in connection with execution of other commands.

BUT you should try out the following 'trick' which gives some of
the functionality you want:

Include the following lines in your .profile (this is Bourne Shell)
or type them in directly if you like:

	trap 'PS1="`fortune` > "' 5
	while true ; do
		sleep 10 ; kill -5 $$
	done &

Now watch what happens after the execution of the next command.

This is not exactly what you asked for, but the idea might be of some
use to you.

--
Kim F. Storm, storm@diku.UUCP (via seismo or mcvax or ...)
Institute of Datalogy(=CS), U of Copenhagen, Universitetsparken 1, DK-2100 OE

herve@titn.UUCP (01/24/87)

Regarding the prompt setting under csh, here is the way I handle that in 
my login scripts, for what it's worth (all this on a VAX 750, 4.2):

The .login file has the following entries:

	alias	cd	'set old=$cwd; chdir \!*; set prompt = "$Ndirs$cwd> "'
	alias	back	'set back=$old; set old=$cwd; cd $back; unset back'
	alias	ndir	"dirs|wc|sed -e 's/^ *1 *//' -e 's/ .*//'"
	alias	push	'pushd \!*; set Ndirs="`ndir` - "; cd .'
	alias	pop	'popd \!*; set Ndirs="`ndir` - "; cd .'
	set	Ndirs =	"`ndir` - "
	cd .

The prompt is then the current directory preceded by the number of directories
currently on the stack (as I never remember what I'm doing with pushd and popd,
that's an information I find quite useful...).  The back command allows me to 
switch between the last two directories I used.

I did not put a "set prompt = xxxx" in my .cshrc script. In this case, the
standard '%' prompt reminds me, when I see it, that I am in a subshell.

For people who heavilly use pushd and popd and who cannot afford the delay
in the execution of ndir, just use:

	alias	cd	'set old=$cwd; chdir \!*; set prompt = "$cwd> "'
	alias	back	'set back=$old; set old=$cwd; cd $back; unset back'
	alias	push	'pushd \!*; cd .'
	alias	pop	'popd \!*; cd .'
	cd .

instead of the above, and push and pop will be faster.

FROM:	Herve Siegrist, TITN Inc.  24301 Southland Dr. Suite 200
	Hayward, CA 94545	Tel: (415) 785-5970
UUCP:	(decvax|ucbvax|ihnp4)!decwrl!sun!dlb!plx!titn!herve

davy@pur-ee.UUCP (01/30/87)

In article <104@titn.TITN> herve@titn.TITN (Herve Siegrist) writes:
>
>	alias	cd	'set old=$cwd; chdir \!*; set prompt = "$Ndirs$cwd> "'
>	alias	back	'set back=$old; set old=$cwd; cd $back; unset back'
>	alias	ndir	"dirs|wc|sed -e 's/^ *1 *//' -e 's/ .*//'"
>	alias	push	'pushd \!*; set Ndirs="`ndir` - "; cd .'
>	alias	pop	'popd \!*; set Ndirs="`ndir` - "; cd .'
>	set	Ndirs =	"`ndir` - "
>	cd .
>

I've seen lots of these "back" aliases, and never really understood why.
The "pushd" command, if given no arguments, pops the directory stack
and pushes the current directory, thus effectively doing exactly the
same thing without all the shell variables.

For example, my .cshrc (it's in .cshrc because suntools doesn't run
"-csh" and so .login won't get run in windows) has:

	if ($?prompt) then
		set cpu = `hostname | sed -e 's/\.*//'`

		alias cd chdir \!:\* \; set prompt='${cpu}:${cwd}\[!\]\ '
		alias pd pushd \!:\* \; set prompt='${cpu}:${cwd}\[!\]\ '
		alias pp popd \!:\* \; set prompt='${cpu}:${cwd}\[!\]\ '

		cd .
	endif

which gives me a prompt with the current hostname (minus the domains,
which in our case is ".ecn.purdue.edu", a little long), the current
directory, and the history number in brackets, as in:

	intrepid:/usr/harbor/davy[23]

To change directories I just do "pd newdir", to pop I just do "pp",
and to bounce between two directories, I just do "pd".

--Dave Curry

serge@imag.UUCP (02/02/87)

In article <2935@diku.UUCP> storm@diku.UUCP writes:
>You cannot use a shell script or a program to change the shell prompt
>dynamically, because the PS1/prompt variables are local to the shell,
>and therefore they can only be changed by the shell itself.  

avec la commande  chdir :    echo directory ?
                             read  dir
                             cd $dir && PS1=`pwd`" $ "

et l'appel de commande  :    . chdir
-- 
================================================================================
serge@imag.UUCP   or {seismo.css.gov|mcvax|inria}!imag!serge
Rouveyrol Serge,Laboratoire L.G.I,BP 68,38402 St Martin d'Heres CEDEX,FRANCE
tel 76 51 46 00 poste 5132

wcs@ho95e.UUCP (02/03/87)

In article <2935@diku.UUCP> storm@diku.UUCP writes:
>In article <432@ethz.UUCP> wyle@ethz.UUCP (Mitchell Wyle) writes:
>>Does anyone out there have a shell script / program to change
>>the prompt DYNAMICALLY after each [CR]?
>
>You cannot use a shell script or a program to change the shell prompt
>dynamically, because the PS1/prompt variables are local to the shell,
>and therefore they can only be changed by the shell itself.  

If all you want to do is set the prompt based on a few variables (process id,
current directory, some-shell-variable, history-number), ksh and csh have some
hooks to do these.  I don't know csh, but I've seen people whose prompt
includes a number incremented by 1 each command; makes it easier to use csh's
boring version of history.

Ksh (which is upward-compatible with sh, and has decent history), allows you to
evaluate variables each time you access PS1.  Thus, my PS1 is set to
	PS1='e!!${PWD}> '
which looks like
	e!/usr/spool/news>
(the ! is doubled to prevent csh-like history-number substitution)
I don't think you can do execution at the prompt (which would be really slow),
but the newer versions of ksh let you set SECONDS to get a crude clock.
	SECONDS=0; export SECONDS
	PS1='Logged in $SECONDS: '
You can also set MAILPATH to check files other than just $MAIL.
-- 
# Bill Stewart, AT&T Bell Labs 2G-202, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs

markp@valid.UUCP (02/12/87)

> 	alias cd 'cd \!*; set prompt=$cwd> '
> 
> 	alias pushd 'pushd \!*; set prompt=$cwd> '
> 	alias popd 'popd; set prompt=$cwd> '

I have found it useful to be able to attach symbolic names to certain
directories during a session, so I have taken this scheme one step farther.
Any shell variable starting with zz is a "mark."  The shell variable zz
corresponds to the last directory you were in.  Here is the initialization
sequence from my .cshrc: (these don't affect the prompt, but that can be added)

	alias cd 'set zz=$cwd;chdir \!*'
	alias b 'set zzZZ=$cwd;chdir $zz\!*;set zz=$zzZZ;unset zzZZ'
	alias m 'set zz\!*=$cwd'
	set zz=$cwd

"cd" saves the current directory in zz and changes directory.
"b" by itself saves the current directory in zz and goes back to the previous
 directory (i.e. the previous value of zz).  Repeated invocations of "b"
 switch back and forth between the 2 most recent directories.
"b argument" saves the current directory in zz and changes to the directory
 zzargument.
"m argument" assigns the current working directory to the "argument" mark,
 i.e. zzargument.

The zz variable is initialized by .cshrc for proper operation of the aliases.
You can also initialize a number of zzvariables in your .cshrc if you want,
effectively giving symbolic names to directories which you frequent.  A
number of people I know do this using one alias per directory, but the m/b
method seems cleaner, and is more convenient to use in practice.

	Mark Papamarcos
	Valid Logic
	{ihnp4,hplabs}!pesnta!valid!markp