Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: cd to a file
- X-seq: zsh-users 11411
- From: Frank Terbeck <ft@xxxxxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: cd to a file
- Date: Mon, 16 Apr 2007 18:00:51 +0200
- In-reply-to: <20070416144016.GA20630@xxxxxxxxx>
- Mail-followup-to: zsh-users@xxxxxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <vlc8d0toqggtc9gvlicp20ugllqlh0f7qa@xxxxxxx> <200406210039.i5L0dsrq022663@xxxxxxxxxxxxxxxxxxxx> <20070416144016.GA20630@xxxxxxxxx>
Andy Spiegl <zsh.Andy@xxxxxxxxx>:
[...]
> # 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.
I once did a 'cd'-wrapper function, that also tries to do some basic
error correction (this is also on <http://zshwiki.org>):
[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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author