Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Convert absolute paths to relative paths
- X-seq: zsh-users 4844
 
- From: "Bart Schaefer" <schaefer@xxxxxxxxxxxxxxxx>
 
- To: Vin Shelton <acs@xxxxxxxxxxxxxxxxxxxx>, zsh-users@xxxxxxxxxx
 
- Subject: Re: Convert absolute paths to relative paths
 
- Date: Fri, 19 Apr 2002 17:51:50 +0000
 
- In-reply-to: <545ofgfaowz.fsf@xxxxxxxxxxxxxxxx>
 
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
 
- References: <545ofgfaowz.fsf@xxxxxxxxxxxxxxxx>
 
On Apr 19,  7:49am, Vin Shelton wrote:
}
} Can anyone show me some nifty zsh hackery to convert a string
} representing an absolute path into a relative path?  For example, if
} my current $PWD is "/opt/build" and the path I want to convert is
} "/opt/src/perl-5.6.1", I would like the result to be
} "../src/perl-5.6.1".
function relative {
    emulate -L zsh
    local up=.. down
    # ! -d $up/$down accounts for symlinks in $PWD
    while [[ ${PWD#$1} == $PWD || ! -d $up/$down ]]
    do
	up=../$up
	if [[ -n $1:t ]]
	then
	    down=$1:t${down:+/$down}
	    1=$1:h
	fi
    done
    print $up/$down
}
-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com
Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   
Messages sorted by:
Reverse Date,
Date,
Thread,
Author