Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!seismo!mimsy!cvl!umd5!zben From: zben@umd5.UUCP Newsgroups: comp.unix.questions Subject: prompt wars Message-ID: <1480@umd5> Date: Sat, 14-Mar-87 17:16:15 EST Article-I.D.: umd5.1480 Posted: Sat Mar 14 17:16:15 1987 Date-Received: Sun, 15-Mar-87 04:49:05 EST Organization: University of Maryland, College Park Lines: 128 I started by picking up and enhancing some aliases originally posted by Chris Torek in article <5150@mimsy.uucp>. I wanted to have the name of the current directory in the prompt, but some amount of its parentage. This precluded use of the :t modifier. Via a combination of aliases I got what I wanted (see below) but it was taking over four SECONDS to do a change directory. This was considered annoying, so I replaced the mess with a C program (at the end of this article). The change directory time is now one second or less. WHAT I WANTED: Current machine (no need for domains) and the last two directory names of the full name of the current directory. Here is an example: umd5[3] ~ % cd /usr/spool/news/comp/unix/questions umd5[4] ...unix/questions % ALIAS, SHELL SCRIPT, AND AWK SOLUTION: in ".cshrc": alias set_prompt 'set prompt="`hnm1`[\\!] `dirs|cdt2` % "' alias cd 'cd \!*; set_prompt' alias pushd 'pushd \!*; set_prompt' alias popd 'popd \!*; set_prompt' set_prompt in "~/bin/hnm1": #! /bin/sh # output user-friendly head of host name hostname | awk -F. '{ print $1 }' in "~/bin/cdt2": #! /bin/sh # output user-friendly tail of CD name awk '{print $1}' | awk -F/ ' (NF>3)||(NF>2)&&(length($1)!=0) {printf "..."} (NF>1) {printf "%s/",$(NF-1)} {printf "%s\n",$NF}' ALIAS AND C PROGRAM SOLUTION: in ".cshrc": alias cd 'cd \!*; set prompt="`dirs|genpro`"' alias pushd 'pushd \!*; set prompt="`dirs|genpro`"' alias popd 'popd \!*; set prompt="`dirs|genpro`"' set prompt="`dirs|genpro`" and the following C program in "~/bin/genpro": /* Generate prompt string * Ben Cranston 3/14/87 * * designed to be called as: * * alias cd 'cd \!*; set prompt="`dirs|genpro`"' * * builds and outputs string: [!] * * where is the name of the current host sans trailing domains * is the current directory, with all but the last two * directory names replaced by ... for brevity. * * I had all this working with "awk" scripts, but it was taking over * four SECONDS to switch directories. This was considered wasteful. * */ #define BUFFSIZE 512 #include /* MAXHOSTNAMELEN */ #include /* string funcs */ main(argc,argv) int argc; char **argv; { char hostname[MAXHOSTNAMELEN]; char dirs[BUFFSIZE], buff[BUFFSIZE]; char *cp; char *p1, *p2; gethostname(hostname,sizeof(hostname)); if ( 0 != (cp=index(hostname,'.')) ) *cp = 0; /* truncate hostname at first period (if any) */ gets(dirs); if ( 0 != (cp=index(dirs,' ')) ) *cp = 0; /* truncate dir string at first space (if any) */ /* search backwards for slash. if found, temporarily make null and * search backwards for slash again. if found again, replace string * to the left of the second slash with "..." * * the additional test of (p1!=dirs) is for a special case, a two-string * explicitly rooted. that is, "cd /etc/ns" will display as /etc/ns * rather than ...etc/ns */ buff[0] = 0; cp = dirs; if ( 0 != (p2 = rindex(dirs,'/')) ) { *p2 = 0; if ( (0 != (p1 = rindex(dirs,'/'))) && (p1 != dirs) ) { strcpy(buff,"..."); cp = p1 + 1; } *p2 = '/'; } strcat(buff,cp); printf("%s[!] %s %% ",hostname,buff); } Needless to say this technique provides a skeleton upon which a variety of personal preferences can be hung. Enjoy! -- umd5.UUCP <= {seismo!mimsy,ihnp4!rlgvax}!cvl!umd5!zben Ben Cranston zben @ umd2.UMD.EDU Kingdom of Merryland UniSys 1100/92 umd2.BITNET "via HASP with RSCS"