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,  3:52pm, Dominik Vogt wrote:
} Subject: a little problem with the second argument to cd
}
}   $ cd foo bar
}   cd: no such file or directory: .../bar/libfoo
} 
} Can I force zsh to consider foo/libbar as a match too (preferrably
} without re-implementing the whole functionality as a script)?

The answer is either "no", or "only by manually clarifying", e.g:

    $ cd bfoo bbar

The two-arg form of cd does _exactly_ what this does:

    cd ${PWD/foo/bar}

There's no "considering" involved, it's a strictly textual replacement
with no reference to the filesystem.

The function to do what you want would look something like this:

    cd2 () {
	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
	cd "${target:-${PWD/${(q)1}/$2}}"
    }



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