(SUMMARY) cd_memory.csh - a cd script that remembers where you wer

From: Dermot Paikkos (dermot@sciencephoto.com)
Date: Wed Apr 24 2002 - 06:58:53 EDT


Hi Managers,
Taken me a while to get this, the 2nd summary, on cd -.
As I said before this is not a standard feature of csh. However there
are work arounds. I have NOT tested any of these myself but thought
they should be shared with the group anyway.
The 1st option is to install tsch.
The 2nd option is the following script submitted by Bob Harris. The
script is is said to store the previous path from CD_HISTORY which
can be called latter.
The 3rd option came from Richard Loken. This is a different
approach. It creates aliases for the old directory so you can call `back
/old/dir` to get back. Again un-tested by myself.

Thanx.
Dp.

---------------------------- Bob Harris's script ---------------
#!/bin/csh
#---------------------------------------------------------------------
#---------
# cd_memory.csh - cshell script to provide a memory function to the
# 'cd'
# command.
#
# The following command needs to be placed in the .cshrc file so
# that a csh alias is properly defined for this command to work.
#
# alias cd 'set CD_ARGS="\!*";source $BIN/cd_memory'
#
# where $BIN is the directory you keep your personal scripts.
#
# There are 2 limitations. cd without args will _NOT_ return
you to
# your home directory. You must either enter 'cd ~' or 'cd
$HOME' or
#type your full home directory path. The numbers between 0-99
        are
#used to select recently visited directories from the cd history, so
#if you have a subdirectory name that is 0-99 you will need to
# specify 'cd ./8' instead of 'cd 8'
#
# Usage:
#
# cd # list the CD_HISTORY
# cd -l # list the CD_HISTORY
# cd -h # list the CD_HISTORY
# cd 1-99 # use previous directory (see cd -h)
# cd path # cd to the specified path
# cd partial # cd to a string that matches part of an entry in the
# history cd ~ # cd to $HOME
#
#---------------------------------------------------------------------
#---------
# Bob Harris
#---------------------------------------------------------------------
#---------
set CD_NEW="$CD_ARGS"
set CD_HISTORY_MAX=99 # shorten history list when it get
beyond this
size set CD_HISTORY_MIN=50 # shorten history to this size # set
unset_variables=(CD_HISTORY_MAX CD_HISTORY_MIN CD_ARGS
CD_NEW) set
unset_variables=($unset_variables n j k old_cwd unset_variables) set
unset_variables=($unset_variables all_set cd_word cd_beginning) #
if (
$?CD_HISTORY != 1 ) then
    set CD_HISTORY="" # initialize the history list.
endif
#
set all_set=0
switch ( "$CD_NEW" )
#
# If nothing, -l, or -h then list the most recent visited
# directories.
#
case "":
case "-l":
case "-h":
    @ n = $#CD_HISTORY
    foreach j ($CD_HISTORY)
 if ( X$j == X/ ) then
     echo "$n $j"
 else
     echo "$n $j/"
 endif
 @ n -= 1
    end
    echo "$n $cwd/"
    set CD_NEW=""
    unset $unset_variables
    exit 0 # Nothing to add to the history, so exit instead of
break.
# # Use the specified previous directory from the history list. #
case [0-9]: case [1-9][0-9]:
    if ( $CD_NEW == 0 ) then
 set CD_NEW=$cwd
    else
 @ n = $#CD_HISTORY
 @ n -= $CD_NEW - 1
 set CD_NEW=${CD_HISTORY[$n]}
    endif
    set all_set=1
    breaksw # break and allow history processing on the way out.
# #
Special case ~ for $HOME # case "~":
    set CD_NEW=$HOME
    set all_set=1
    breaksw # break and allow history processing on the way out.
endsw # # See if this is a directory in the CDPATH # if ( "$CD_NEW"
!~
. && "$CD_NEW" !~ ./* && "$CD_NEW" !~ .. && "$CD_NEW" !~ ../*
&&
"$CD_NEW" !~ /* && "$CD_NEW" !~ ~* ) then
    if ( $all_set == 0 ) then
 if ( $?cdpath == 1 ) then
     foreach j ($cdpath)
  if ( -d "$j/$CD_NEW" ) then
      if ( "$j" == . ) then
   set CD_NEW="$cwd/$CD_NEW"
      else
   set CD_NEW="$j/$CD_NEW"
      endif
      set all_set=1
      break
  endif
     end
 endif
    endif
    #
    # See if this is a partial match in the CD_HISTORY. The more
    recent # entries at the end of the list are more important that
    the older # ones. So we search the complete list (including the
    current directory) # saving the last match in 2 catagories. The
    first is whole words as in # fship/src or src when they match the
    entire directory name deliminated # by a slash. The second is if
    it matches the beginning of the last # directory name in the path.
     When we are all done, always choose the # whole word match
over
    the beginning of the last directory match. # set cd_word="" set
    cd_beginning="" if ( $all_set == 0 ) then
 foreach j ($CD_HISTORY $cwd)
     if ( "$j" =~ */$CD_NEW ) then
  if ( -d "$j" ) then
      set cd_word="$j"
  endif
     else if ( "$j:t" =~ $CD_NEW* ) then
  if ( -d "$j" ) then
      set cd_beginning="$j"
  endif
     endif
 end
    endif
    if ( X$cd_word != X ) then
 set CD_NEW=$cd_word
 set all_set=1
    else if ( X$cd_beginning != X ) then
 set CD_NEW=$cd_beginning
 set all_set=1
    endif
endif
#
# Save the old directory
#
set old_cwd="$cwd"
#
# Store the old directory in the history list
#
if ( -d $CD_NEW ) then
    if ( "X$old_cwd" != "X$CD_NEW" ) then
 set CD_HISTORY=($CD_HISTORY $old_cwd)
    endif
endif
#
# If the history list has gotten too big, then throw away enough old
# entries until there are CD_HISTORY_MIN directories left on the list.
#
if ( $#CD_HISTORY > $CD_HISTORY_MAX ) then
    @ n = $#CD_HISTORY
    set k=""
    foreach j ($CD_HISTORY)
 if ( $n <= $CD_HISTORY_MIN ) then
     set k=($k $j)
 endif
 @ n -= 1
    end
    set CD_HISTORY=($k)
endif
#
# Check if this is a file. If it is, then strip off the file name and
# just use the directory.
#
if ( -f $CD_NEW ) then
    set CD_NEW=`/bin/dirname $CD_NEW`
endif
#
# Echo the new directory (this is just an aid to the user).
#
chdir $CD_NEW
pwd
unset $unset_variables
exit 0

------- End of Bob Harris's script -------

------- Start if Ricjead Loken text ---------
This is one way to do it and its the way we have done it here for 20
years:

    alias cd 'set old=$cwd; chdir \!*;echo $cwd'
    alias back 'set back=$old; set old=$cwd; cd $back; unset back'

you then can say:

 % cd /foo/baz
 /foo/baz
 % back
 /old/dir
 %

If you use Xwindows on Xterms or something like that then the cd
alias can
be:

    set hostid = `hostname|cut -f1 -d.`
    alias iam 'echo -n "^[]2;\!*^G"'
    alias cd 'set old=$cwd; chdir \!*;echo $cwd; iam
    "root@${hostid}:$cwd"'

And it will put the default directory into the top bar of your Xterm
window.

When I moved over to VMS this was the first thing I ported but it
became
30 lines of DCL instead of two aliases.

--------------- End of Richard Loken --------------
~~
Dermot Paikkos * dermot@sciencephoto.com
Network Administrator @ Science Photo Library
Phone: 0207 432 1100 * Fax: 0207 286 8668



This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:48:39 EDT