Taming the Terminal book cover

Bruce from Tennessee Explains CDPATH

Hi. This is Bruce from East Tennessee, aka UseTheData, with a quick terminal tip about something called CDPATH.

The problem to be solved is that I work in the terminal quite a bit, and I want to quickly get into my project folders, which are largely in Dropbox. In the terminal, we use the cd command to do this, as Bart and Allison discussed way back in Taming the Terminal episode 4. However, Apple changed a lot of things about where cloud storage files are located. My Dropbox folder used to be at the root of my user folder (i.e. ~/Dropbox), but it’s now at ~/Library/CloudStorage/Dropbox. Instead of getting to my current projects with cd ~/Dropbox/Current-Projects, I would have to type cd ~/Library/CloudStorage/Dropbox/Current-Projects, which is a lot longer.

I say “would have to type” because one of the ways to solve this problem is something called CDPATH. CDPATH is a terminal environment variable, similar to the PATH variable that Bart and Allison described in Taming the Terminal episode 13, except that CDPATH works with the cd command, rather than the way PATH is used to make getting to executable files easier.

I’ll go into the details in a minute, but the result is that I can now type cd Current-Projects and land in the folder for all of my current projects, regardless of where I am in the file system. I can also go directly to the folder for any individual current project. For example, I have a project to track the maintenance work I do around the house. I can now type cd Maintenance, and that will land me in the folder for that project, regardless of where I am in the file system.

How do I do this?

The answer is a line in my .zshrc that sets the CDPATH variable. For me, that line is

export CDPATH=".:$HOME/Library/CloudStorage/Dropbox:$HOME/Library/CloudStorage/Dropbox/Current-Projects:$HOME/Documents"

The result of setting this variable is that when I type cd <folder-name>, the cd command will first look in the current folder — that’s the dot at the start of the CDPATH variable. It will then look for that folder at the root of my Dropbox. It will then look for that folder in my Current-Projects folder. And lastly, it will look for that folder in my Documents folder.

But wait, it gets better.

In the default macOS configuration with zsh, I have to type the full folder name, such as “Current-Projects” to get there. But just the first three letters C-u-r are unique. What if I could just type cd Cur then hit the tab key and land in that Current-Projects folder?

It turns out that I can, though it involves a pretty arcane incantation.

The answer is to add these two lines to your .zshrc file:

autoload -Uz compinit
compinit

I’m not going to go into why this works. The Valuable Dev’s article on zsh completion goes into a lot of details. I have been through this, and I’m comfortable that I understand why this enables the tab completion for folder names with the cd command. But it would be a definite propeller head episode of Taming the Terminal to explain it. For what it’s worth, I didn’t have to enable compinit on my Ubuntu system. Tab completion with cd just worked. Which is what led me to go hunting for a way to make it work on my Mac.

This is definitely a bit on the nerdy side, but it’s a very useful tool for moving around the file system if you work in the terminal.

Peace, and may you find beauty in the world around you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top