ZSH Gem #20: Changing directories the pro's way
I guess, the cd command is the most often neglected command. Nobody really cares about what this command can do, but in fact, ZSH has added some magic to it, which makes it worth a more thorough look.
I told you about AUTO_CD before, a shorthand syntax for the cd command. But that's not the only thing ZSH has done to cd.
The first exciting feature I want to tell you about is changing directories by replacing parts of the current path name. That sounds abstract, but let me explain. It's actually quite simple. Imagine, you are in your home directory /home/johndoe and you need to change to the home directory /home/janedoe. You could easily do that with
cd ../janedoe
or with AUTO_CD enabled
../janedoe
That's already nice and short, but have a look at this:
cd john jane
If you like, you could even omit the j as it's the same in both names and run just
cd ohn ane
What the command does is obvious: it replaces the first occurrence of the first string in the current path name with the second string. The string can appear anywhere in the path name. As long as the newly generated path is a valid directory, it will work. If a replacement is ambiguous (e.g. if there is also a home directory /home/janetdoe where cd john jane could either switch to Jane's or Janet's home directory), the match which comes first in the alphabet is taken.
cd johndoe/john janedoe/jane
switches from /home/johndoe/john to /home/janedoe/jane and for instance
cd usr usr/local
can change from something like /usr/share/applications to /usr/local/share/applications without typing any unnecessary characters.
That is the first cd pro feature I want to show you. The second one is working with the directory stack. Just like Bash, ZSH has a so called directory stack. That's a list of directories you can work with. The top-most entry is always the current working directory. You can view the current directory stack with
dirs -v
To work with the directory stack, you can use pushd DIR to change to the directory DIR and place it on top of the stack or popd to remove the top-most entry from the stack and automatically change to it. popd can also take an argument +n or -n where n is a number defining a specific entry counting from the top or the bottom.
That's all the same Bash as in ZSH and to be honest: I've never worked with it because using popd and pushd is quite cumbersome and I'm faster without (defining aliases for frequently used directories is more convenient). As I told you, the top-most entry is always the current working directory, but normally when you cd to another directory, the new working directory isn't put on top of the stack. Instead it replaces the last entry for the CWD. To really add directories to the stack you have to do it explicitly with pushd.
However, in ZSH you can work with the directory stack directly with cd. If the option AUTO_PUSHD is set, the current working directory is automatically added permanently to the stack. That means, the stack contains the whole history of all the directories you've been in during this session. To prove that, make sure AUTO_PUSHD is set, navigate through some directories as you'd normally do and then enter dirs -v. You should see all the directories you just skipped through in the stack (directories you entered twice are not saved again but moved to the top of the stack).
Now that you have the stack, you can use cd to navigate between its entries. Use the normal popd argument syntax for it:
cd +4
cd's to the fourth entry from the top.
cd -2
goes to the second entry from the bottom. No need for manual pushd or popd calls. Just use cd {+|-}n to switch between the entries and let ZSH handle the stack by itself. Don't bother about that anymore.
Read more about cd and directory stacks:
Really cool zshell spells I recently stumbled on https://t.co/ZcTrlUSGbh