zsh-users
 help / color / mirror / code / Atom feed
From: Andy Spiegl <zsh.Andy@spiegl.de>
To: zsh-users@sunsite.dk
Subject: cd to a file
Date: Mon, 16 Apr 2007 16:40:16 +0200	[thread overview]
Message-ID: <20070416144016.GA20630@spiegl.de> (raw)
In-Reply-To: <200406210039.i5L0dsrq022663@bang.bigasterisk.com>

Hi,

a long while ago Drew Perttula <drewp@dot.bigasterisk.com> posted this very
useful function:

> # cd to a file should take you to the dir that contains the file
> # courtesy of Artur Penttinen <artur@niif.spb.su>
> 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@niif.spb.su>
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@spiegl.de
-- 
 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.


  reply	other threads:[~2007-04-16 14:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-19 12:43 More zsh tricks required please zzapper
2004-06-19 13:06 ` Christian Schneider
2004-06-20 12:19 ` Julius Plenz
2004-06-21  0:38 ` Drew Perttula
2007-04-16 14:40   ` Andy Spiegl [this message]
2007-04-16 16:00     ` cd to a file Frank Terbeck
2007-04-18  1:08       ` Andy Spiegl
2007-04-21 15:12       ` Atom Smasher
2007-04-22 18:18         ` Frank Terbeck
2007-04-23  8:45           ` Atom Smasher

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070416144016.GA20630@spiegl.de \
    --to=zsh.andy@spiegl.de \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).