Friday, June 15, 2012

Bash: switch folders quickly with $CDPATH

Most unix users have a few favorite folders for storing source repositories and other stuff they work on daily. There is a feature in Bash that makes it very easy to switch to these folders and it's very simple to use. Just add these folders to the CDPATH environment variable.

e.g. put this in your .profile 
export CDPATH=:~/src:~/work

Different folders are separated by colon. A colon at start or end is shortcut for indicating the home directory. 

What you get:
  1. You can switch to any folders under the folders specified in $CDPATH by just providing the folder name without specifying the full path. e.g.
    to switch to "~/src/graphite" you can just say "cd graphite"
  2. You also get tab completion based on folders under CDPATH folders. This is incredibly useful.
  3. If a folder with same name is present in multiple locations then the current directory gets the highest preference followed by the order of folders specified in $CDPATH. This makes sure that CDPATH doesn't confuse your existing workflow in any way, as current directory always gets priority.
This is one of those tricks you spend five minutes on one day and then enjoy the benefits for the rest of your life.

No comments:

Post a Comment