ZSH Gem #20: Changing directories the pro's way

Posted by | Comments (3) | Trackback (1)

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:

Trackbacks

Comments

There have been 3 comments submitted yet. Add one as well!
Robert Sawko
Robert Sawko wrote on : (permalink)
Thanks! Finally, I got the gist on how to use directory stack conveniently. That's a great tip and thanks for sharing.
Jyothis
Jyothis wrote on : (permalink)
I just want to add that typing cd - and hitting TAB will show the list of directories in the directory stack and their entry numbers.
Tarun Elankath
Tarun Elankath wrote on : (permalink)
Even More Pro ways: - Use https://github.com/rupa/z - Use https://github.com/wting/autojump - Use https://github.com/junegunn/fzf

Write a comment:

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

By submitting a comment, you agree to our privacy policy.

Design and Code Copyright © 2010-2024 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms.