From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18385 invoked from network); 16 Apr 2007 16:02:12 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.8 (2007-02-13) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,FORGED_RCVD_HELO autolearn=ham version=3.1.8 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 16 Apr 2007 16:02:12 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 3785 invoked from network); 16 Apr 2007 16:02:07 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 16 Apr 2007 16:02:07 -0000 Received: (qmail 24494 invoked by alias); 16 Apr 2007 16:01:48 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 11411 Received: (qmail 24483 invoked from network); 16 Apr 2007 16:01:45 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 16 Apr 2007 16:01:45 -0000 Received: (qmail 2176 invoked from network); 16 Apr 2007 16:01:43 -0000 Received: from mta-2.ms.rz.rwth-aachen.de (134.130.7.73) by a.mx.sunsite.dk with SMTP; 16 Apr 2007 16:01:40 -0000 Received: from r220-1 ([134.130.3.31]) by mta-2.ms.rz.RWTH-Aachen.de (Sun Java System Messaging Server 6.2-7.05 (built Sep 5 2006)) with ESMTP id <0JGL00F8CLTJ34D0@mta-2.ms.rz.RWTH-Aachen.de> for zsh-users@sunsite.dk; Mon, 16 Apr 2007 18:00:55 +0200 (CEST) Received: from relay.rwth-aachen.de ([134.130.3.1]) by r220-1 (MailMonitor for SMTP v1.2.2 ) ; Mon, 16 Apr 2007 18:00:55 +0200 (MEST) Received: from fsst.voodoo.lan ([212.117.84.9]) by relay.rwth-aachen.de (8.13.7/8.13.3/1) with ESMTP id l3GG0s8M026990 for ; Mon, 16 Apr 2007 18:00:54 +0200 (MEST) Received: from hawk by fsst.voodoo.lan with local (Exim 4.63) (envelope-from ) id 1HdTdL-0001dS-EK for zsh-users@sunsite.dk; Mon, 16 Apr 2007 18:00:51 +0200 Date: Mon, 16 Apr 2007 18:00:51 +0200 From: Frank Terbeck Subject: Re: cd to a file In-reply-to: <20070416144016.GA20630@spiegl.de> To: zsh-users@sunsite.dk Mail-followup-to: zsh-users@sunsite.dk Message-id: <20070416160051.GB28724@fsst.voodoo.lan> MIME-version: 1.0 Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 7BIT Content-disposition: inline References: <200406210039.i5L0dsrq022663@bang.bigasterisk.com> <20070416144016.GA20630@spiegl.de> User-Agent: Mutt/1.5.14+cvs20070403 (2007-04-02) Andy Spiegl : [...] > # cd to a file should take you to the dir that contains the file > # courtesy of Artur Penttinen > 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. I once did a 'cd'-wrapper function, that also tries to do some basic error correction (this is also on ): [snip] function smart_cd () { if [[ -f $1 ]] ; then [[ ! -e ${1:h} ]] && return 1 print correcting ${1} to ${1:h} builtin cd ${1:h} else builtin cd ${1} fi } function cd () { setopt localoptions setopt extendedglob local approx1 ; approx1=() local approx2 ; approx2=() if (( ${#*} == 0 )) || [[ ${1} = [+-]* ]] ; then builtin cd "$@" elif (( ${#*} == 1 )) ; then approx1=( (#a1)${1}(N) ) approx2=( (#a2)${1}(N) ) if [[ -e ${1} ]] ; then smart_cd ${1} elif [[ ${#approx1} -eq 1 ]] ; then print correcting ${1} to ${approx1[1]} smart_cd ${approx1[1]} elif [[ ${#approx2} -eq 1 ]] ; then print correcting ${1} to ${approx2[1]} smart_cd ${approx2[1]} else print couldn\'t correct ${1} fi elif (( ${#*} == 2 )) ; then builtin cd $1 $2 else print cd: too many arguments fi } [snap] Regards, Frank -- In protocol design, perfection has been reached not when there is nothing left to add, but when there is nothing left to take away. -- RFC 1925