Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

completing second argument of cd



I don't remember seeing a function like this posted before... it's for
use with compctl and completes the second argument of cd, i.e. it
tries to find a possible new directory by altering an occurrence of
the existing cd argument in $PWD.

e.g.

% pwd
/home/sgi/pws/lgt/su2/phase/data/12_6/2.4
% cd 12_6 <TAB>      ->      cd 12_6 8_4
~/lgt/su2/phase/data/8_4/2.4

(yes, I wrote this to save typing three characters :-))

The comment about the bug refers to this:
% pwd
/groupfs/schierholz3/stephens/b6p00kp1550.bub/rho
% cd rho pion
cd: no such file or directory:
/groupfs/schiepionlz3/stephens/b6p00kp1550.bub/rho

(I've split the error message):  in other words, there's a perfectly
good directory /groupfs/schierholz3/stephens/b6p00kp1550.bub/pion, but
the shell has decided willy-nilly that the first rho it finds is the
one to change.  It's a philosophical bug, if you consider zsh is
supposed to be smart :-).

Here's the function.


# cdswap:  function to be used for completion of second argument of cd
# or pushd.
# Usage:
#   compctl -g '*(-/)' -x 'p[2]' -K cdswap -- cd pushd
# May be combined with $CDPATH searching functions, e.g.:
#   compctl -g '*(-)' -x 'p[2]' -K cdswap -- + -K cdmatch -S / -q cd pushd
# Peter Stephenson (pws@xxxxxx)

# reset options after use:  no reply if no match
setopt localoptions nullglob
# stripped bits of PWD not to be eligible for globbing:
# use zsh-style arrays
unsetopt globsubst ksharrays

local args
# args looks like (cd old new)
read -Ac args

# Always use first match for $args[2] (this is how cd works regardless
# of whether resulting directory exists --- is this a bug?)
reply=(${PWD%%$args[2]*}*${PWD#*$args[2]}~$PWD)
reply=(${${reply#${PWD%%$args[2]*}}%${PWD#*$args[2]}})


-- 
Peter Stephenson <pws@xxxxxx>       Tel: +49 33762 77366
WWW:  http://www.ifh.de/~pws/       Fax: +49 33762 77330
Deutches Electronen-Synchrotron --- Institut fuer Hochenergiephysik Zeuthen
DESY-IfH, 15735 Zeuthen, Germany.



Messages sorted by: Reverse Date, Date, Thread, Author