Imagine you are working in your shell, you could be in any directory anywhere, but you want to go to
You’re probably used to typing something like this:
Wouldn’t it be nice if you could end up in the exact folder you want with just:
CDPATH environment variable
Much like the
PATH environment variable defines a search order for the executable you want,
CDPATH defines the order for directories to search to when you use
PATH, directories are separated with a
PATH, where you want to avoid letting an executable in the current directory be “default”, you will probably want to list
. (the current directory) first in your
Modifying your shell environment
~/.zshrc, or applicable “environment script”.
Add the following line using whatever folders you want to search:
# search current directory, home directory, and projects folder export CDPATH=".:~:~/Projects"
After changing your environment startup script, you can
source ~/.bash_profile to load the changes into your current shell environment.
Enabling Tab Completion in bash
If you are a zsh user, tab completion inside of folders in your
CDPATH should already work.
If you have an older version of bash and it isn’t working, you will need to download and install bash-completion.
You can install it via your favorite package manager; apt, brew, macports, rpm etc all seem to have a package for it.
You might be required to edit your
~/.bash_profile again to make it work, but it’s worth it.
Why didn’t I know this?!
I’ve been using POSIX shells for over 20 years now, and it always surprises me when I learn about a feature I’ve been missing out on for all of them.
Today’s revelation came from fellow Bocouper Tim Branyen who remarked in an IRC channel that his mind had been blown.
Mine was too, so I wanted to share this quick tip, but now I’m going to
cd MyPro[tab] and get back to work.
I hope you’ll find traversing back to work in your shell easier too.
We moved off of Disqus for data privacy and consent concerns, and are currently searching for a new commenting tool.
Another helpful hack:
alias ..=’cd ..’
alias …=’cd ../../’
alias ….=’cd ../../../’
alias …..=’cd ../../../../’
Yeah, Mathias Bynens\u2019 dotfiles (linked above by timmywil) have that as part of the tasty goodness. My dotfiles are an opinionated fork of those.
I have my own dotfiles repo as well, but I’m a minimalist with my customization. Every now and then I’ll add a new one, but for the most part the basics just work well for me 🙂
I agree…in general I prefer aliases over modifying PATH
This would be a great addition to https://github.com/mathiasb….
https://github.com/rupa/z works without the configurations of your common paths. And is generally just awesome.
It looks pretty cool, but my need for something like z is rare. 99% of the time, I just want a quick cd into to a ~/Projects folder. If that started getting polluted with every other directory I travel through, I feel this would get annoying 🙂