mardi 31 mars 2015

Saving time when you change directories: the CDPATH shell variable

I've been a shell user for more than a decade and I wonder why I haven't heard from CDPATH sooner. If you feel like me, please read on.

If you already know about the PATH variable, you'll find the CDPATH variable usage trivial. The PATH variable is a list of directories where the shell looks for commands. The CDPATH variable is the same but for the cd command and directories.

Set it's value to a list of frequently accessed directories. For example, in my ~/.bashrc or ~/.zshrc:

export CDPATH=.:~/github:/data/web:~/.vim/bundle

If I want to access my dotfiles directory in ~/github, instead of typing:

cd ~/github/dotfiles

Now I just type:

cd dotfiles

Tab completion even works if you have it configured in zsh and if you've installed bash-completion. So now, I only type:

cd dot<Tab><Enter>

and that's it, I'm in the ~/github/dotfiles directory.

Be aware of two things:

Not like PATH, it is important that CDPATH contains the current directory ".". Otherwise, you'll have to type cd ./mydir

Like PATH, CDPATH will take the first value that matches. If I have a dotfiles directory in ~/github and /data/web, cd dotfiles will take me in ~/github/dotfiles

Aucun commentaire: