Join my Laravel for REST API's course on Udemy 👀

Quick cd'ing in ZSH by setting a `cdpath`

March 29, 2021  ‐ 1 min read

There are directories I am cd'ing to more often than others. When my early Linux days when I was using the Bash shell I often used cd commands like: cd ../../../Code/blog. For me, ~/Code is where all my development projects are located, so I often switch to subdirectories of this ~/Code directory. The ZSH shell allows you to add other directories to the search path of the cd, by doing this you can cd into subdirectories of ~/Code (in my case) from any location.

In order to add directories to the search path of the cd command you should set cdpath in your ~/.zshrc.

# ~/.zshrc
setopt auto_cd
cdpath=($HOME/Code)

By adding the above, you are able to directly change to a subdirectory of ~/Code from any location. You should see these subdirectories too in your autosuggestion and get tabcompletion as well.

You can add as many directories to the cdpath array as you would like:

# ~/.zshrc
setopt auto_cd
cdpath=($HOME/Code $HOME/.config)