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

Re: a little problem with the second argument to cd



On Sep 24,  6:16pm, Dominik Vogt wrote:
> 
> Well, I thought someone else might be interested in my cd
> function:
> 
> 1 Argument
> 
>   - If the argument is a directory name it cds into it.
>   - If the name ends with a colon, and a directory with the colon
>     stripped from the name exists, it cds into it.  (Usefull for
>     cut-and-paste).
>   - If the argument is a file and the file name ends in .tar.gz,
>     .tar.bz2, .tgz, .TGZ, .zip or .ZIP, it strips the suffix and
>     cds into the directory with that name (if there is one).
>   - If the argument is a plain file, it cds into the directory in
>     which the file resides.
>   - If all the above attempts fail, works like "builtin cd $1".

This sounds very interesting, but it looks like your implementation
could be a bit more zsh-ish.  No reason to process-substitute with
dirname and basename when zsh can do that stuff itself!

	local DIR="$1:h"
	local STRIP="$1:r"
	local EXT="$1:e"

	if [[ "$EXT" == .(gz|bz2) && "$STRIP" == *.tar ]]; then
	    STRIP="$STRIP:r"
	    EXT=".tar$EXT"
	fi
	if [[ -d "$1" ]]; then
	    builtin cd "$1"
	elif [[ "$EXT" == .(tar.(gz|bz2)|tgz|zip|TGZ|ZIP) &&
		-d "$STRIP" ]]; then
	    builtin cd "$STRIP"
	elif [[ -f "$1" ]]; then
	    builtin cd "$DIR"
	else
	    builtin cd "$1"
	fi



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