Some cool Zsh magic
If you’re a programmer, chances are you spend a lot of time deep inside some nested folder hierarchy. Oftentimes you want to jump back to some fixed point within that hierarchy, but you’re not exactly sure how many levels deep you are, so you just
cd ../../..., using tab completion to guide you until you land where you want.
I find this annoying.
Ideally I would want to have some syntax like
cd .../foo which would go back as many levels as needed until it finds
/). If you’re using Bash, your best bet is to write a wrapper for
cd which does exactly that. If you’re using Zsh (and you should be!) you can be much more clever. Add the following to your
This is basically a high-powered alias that will use extended glob matching until it finds the shortest path of the form
$1 is the first argument. The double-hashes ensure that at least one occurrence of
(../) has to be there (otherwise it could match something in the current working directory). With that all set up, it’s a simple matter of:
Good! But not quite good enough. This works for navigating with
cd, but any other tool which uses paths would need their own command to pull off a similar trick. If you’re using Bash, this is really the end of the line for you. Zsh users, however, can
man zshall, grep for
zsh_directory_name, and get a big smile on their face.
zsh_directory_name is a shell function that basically allows you to process any path entered in the form
~[path] in the substitution phase. With this facility, we can add the following to
n‘ string checks whether
zsh_directory_name is being called to do directory expansion (
n) or directory naming (
d). If so, the reply array is basically doing the same trick
cdd does on
Once we have that, we can operate on any token: