For years I've had some fairly hacky ways of changing directories quicker. I'd wager a nickel these kind of aliases are pretty common:
alias cdblog='cd ~/sites/my_blog'
alias cdapp='cd ~/repos/my_app'
That grows pretty unwieldy when working with a bunch of repos. So then I thought I was super clever by using bash functions to cd directly into a git repo, since all my repos were in one of two parent dirs:
function repos(){ cd ~/repos/$1; }
function sites(){ cd ~/sites/$1; }
me@local$ repo my_app
me@local$ sites my_blog
It was a bit more typing, but fewer aliases to remember and more scalable. However the loss of tab completion using this method is a bummer.
When I finally got annoyed enough to look for a better way, of course it was waiting for me in a superuser post. Using the CDPATH environment variable and the bash-completion
package (same name on centos/fedora/os x/ubuntu) I can now cd into my most used directories, complete with tab completion, from anywhere on the cli.
-
Install bash-completion, substituting appropriate package manager:
brew install bash-completion
-
OS X ONLY: append to your ~/.bash_profile:
if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi
-
Everyone append to your ~/.bash_profile:
export CDPATH=.:~:~/repos:~/sites
-
Activate changes:
source ~/.bash_profile
-
Kick the tires, from say /tmp:
me@local tmp$ cd my<TAB><TAB> my_blog/ my_app/
Tab completion should be working as you'd expect against all your favorite directories, no matter where you are on the cli. This made navigating around dozens of repos quite a bit easier for me, though for a handful of my most used ones I may still treat myself to a dedicated alias. Because I'm weak.