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

Re: a little problem with the second argument to cd



Well, I thought someone else might be interested in my cd
function:

0, 3 or more arguments:

  Works like "builtin cd"

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".

2 Arguments

  As discussed above.

Ciao

Dominik ^_^  ^_^
function cd ()
{
	case $# in
	    0)
		builtin cd
		;;
	    1)
		VAR="$1"
		DIR=`dirname "$1"`
		if [[ -d "$1" ]]; then
		    builtin cd "$1"
		elif STRIP="$DIR/`basename \"$VAR\" :`" &&
			[[ -d "$STRIP" ]]; then
		    builtin cd "$STRIP"
		elif STRIP="$DIR/`basename \"$VAR\" .tar.gz`" &&
			[[ -d "$STRIP" ]]; then
		    builtin cd "$STRIP"
		elif STRIP="$DIR/`basename \"$VAR\" .tar.bz2`" &&
			[[ -d "$STRIP" ]]; then
		    builtin cd "$STRIP"
		elif STRIP="$DIR/`basename \"$VAR\" .tgz`" &&
			[[ -d "$STRIP" ]]; then
		    builtin cd "$STRIP"
		elif STRIP="$DIR/`basename \"$VAR\" .TGZ`" &&
			[[ -d "$STRIP" ]]; then
		    builtin cd "$STRIP"
		elif STRIP="$DIR/`basename \"$VAR\" .zip`" &&
			[[ -d "$STRIP" ]]; then
		    builtin cd "$STRIP"
		elif STRIP="$DIR/`basename \"$VAR\" .ZIP`" &&
			[[ -d "$STRIP" ]]; then
		    builtin cd "$STRIP"
		elif [[ -f "$1" ]]; then
		    builtin cd "$DIR"
		else
		    builtin cd "$1"
		fi
		;;
	    2)
		integer i=0
		local target="$1"
		if ((ARGC > 1)); then
			while ((++i)) &&
				[[ "$PWD" != "${(SI-$i-)PWD#${(q)1}}" ]]
			do
				target="${(SI-$i-)PWD/${(q)1}/$2}"
				[[ -d "$target" && -r "$target" ]] && break
				target="${(SI-$i-)PWD//${(q)1}/$2}"
				[[ -d "$target" && -r "$target" ]] && break
				target=
			done
		fi
		builtin cd "${target:-${PWD/${(q)1}/$2}}"
		;;
	    **)
		builtin cd $*
		;;
	esac
}


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