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

cd to a file



Hi,

a long while ago Drew Perttula <drewp@xxxxxxxxxxxxxxxxxxx> posted this very
useful function:

> # cd to a file should take you to the dir that contains the file
> # courtesy of Artur Penttinen <artur@xxxxxxxxxxx>
> function cd () {
>   if [[ -f $1 ]]; then
>     builtin cd $1:h
>   else
>     builtin cd $1
>   fi
> }

I just noticed that it breaks cd substitution (cd with 2 arguments)
and patched it like this:

# cd to a file should take you to the dir that contains the file
# courtesy of Artur Penttinen <artur@xxxxxxxxxxx>
function cd () {
  if [[ -n $2 ]]; then
    builtin cd $1 $2
  elif [[ -f $1 ]]; then
    builtin cd $1:h
  else
    builtin cd $*
  fi
}

Could still be optimized though, I guess.
 Andy.

-- 
 Fotos: familie.spiegl.de   Info: peru.spiegl.de
 Weblog: blog.spiegl.de     Mail: andy@xxxxxxxxx
-- 
 Finagle's Second Law:
   No matter what the anticipated result, there will always be
   someone eager to (a) misinterpret it, (b) fake it, or (c) believe
   it happened according to his own pet theory.



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