I generally keep my development projects under a common directory like ~/dev/
or ~/projects/
. Here is a simple bash shell script function which I use to quickly jump into and between projects on the command line. It includes auto completion, which is a pretty important part of its usefulness. You can put in your ~/.bashrc
file and adapt the PROJECTS_PATH
variable to suit your own needs.
PROJECTS_PATH=~/dev
pcd() {
if [ "$1" = .. ]; then
local reporoot=$(git rev-parse --show-toplevel 2>/dev/null)
[ -d "$reporoot" ] && cd "$reporoot" || cd ..
else
cd "$PROJECTS_PATH"
[ -d "$1" ] && cd "$1"
[ -d "$2" ] && cd "$2"
fi
}
_pcdcomp() {
local IFS=$'\n'
if [ "$COMP_CWORD" -eq 1 ]; then
COMPREPLY=($(cd "$PROJECTS_PATH" && compgen -d "${COMP_WORDS[1]}"))
elif [ "$COMP_CWORD" -eq 2 ]; then
COMPREPLY=($(cd "$PROJECTS_PATH"/"${COMP_WORDS[1]}" 2>/dev/null && compgen -d "${COMP_WORDS[2]}"))
else
COMPREPLY=()
fi
}
complete -o filenames -F _pcdcomp pcd
After loading the code, type pcd
and hit TAB to see completion of all sub directories. Hit ENTER to jump to a project. It will also complete one level into a single project as second argument. Lastly pcd ..
will jump up to the project root directory, if inside a git repository.
