: # NAME: # ksh.cdhist - cd history for ksh # # DESCRIPTION: # This package provides a nice cd history mechanism for ksh. # #.nf # # cd -l lists history # cd -"n" cd to "n"th directory in list #.fi # # It is pretty simple to use. # # BUGS: # It will not work with pd-ksh prior to v5. # # RCSid: # $Id: ksh.cdhist,v 1.1.1.1 2002/01/15 09:16:22 sjg Exp $ alias cd=_cd # incase $HOME is not writeable... if [ "$CDHISTFILE" ]; then [ -w `dirname $CDHISTFILE` ] || CDHISTFILE=/tmp/.cd_hist$$ fi _cd() { typeset -i cdlen i typeset t if [ $# -eq 0 ] then set -- $HOME fi if [ "$CDHISTFILE" -a -r "$CDHISTFILE" ] # if directory history exists then typeset CDHIST i=-1 while read -r t # read directory history file do CDHIST[i=i+1]="$t" done <$CDHISTFILE fi if [ "${CDHIST[0]}" != "$PWD" -a "$PWD" != "" ] then _cdins # insert $PWD into cd history fi cdlen=${#CDHIST[*]} # number of elements in history case "$@" in -) # cd to new dir if [ "$OLDPWD" = "" ] && ((cdlen>1)) then 'print' ${CDHIST[1]} 'cd' "${CDHIST[1]}" _pwd else 'cd' "$@" _pwd fi ;; -l) # print directory list typeset -R3 num ((i=cdlen)) while (((i=i-1)>=0)) do num=$i 'print' "$num ${CDHIST[i]}" done return ;; -[0-9]|-[0-9][0-9]) # cd to dir in list if (((i=${1#-})=cdlen)) then 'cd' "$@" _pwd fi ;; *) # cd to new dir 'cd' "$@" _pwd ;; esac _cdins # insert $PWD into cd history if [ "$CDHISTFILE" ] then cdlen=${#CDHIST[*]} # number of elements in history i=0 while ((i$CDHISTFILE fi } # insert $PWD into cd history - meant to be called only by _cd _cdins() { typeset -i i ((i=0)) while ((i<${#CDHIST[*]})) # see if dir is already in list do if [ "${CDHIST[$i]}" = "$PWD" ] then break fi ((i=i+1)) done if ((i>22)) # limit max size of list then i=22 fi while (((i=i-1)>=0)) # bump old dirs in list do CDHIST[i+1]=${CDHIST[i]} done CDHIST[0]=$PWD # insert new directory in list } _pwd() { if [ -n "$ECD" ] then pwd 1>&6 fi }