Multiple Project Path Management with Bash
At any given time in the past few years, I’ve had a few projects active at the same time, from little experiments to forks of open source projects to code for various jobs. Over time, I’ve developed a little bash ditty to help me manage switching between projects, and I wanted to share that today.
At the bottom of my ~/.profile:
function project {
PROJECT_BASE=~/code
if [ "$1" ]; then
PROJECT=$PROJECT_BASE/$1
if [ -d $PROJECT ]; then
touch $PROJECT_BASE/$1
elif [ $1 = "list" ]; then
echo
echo "Projects"
echo
ls -t1 $PROJECT_BASE
return 0
else
echo "No project $1"
return -1
fi
fi
cd $PROJECT_BASE/`ls -t1 $PROJECT_BASE|head -n1`
}
project
And, if you have bash completion installed and configured, add the following to a file in ~/.bash_completion.d:
_project() {
COMPREPLY=( $(compgen -W "list `ls ~/code`" --\
${COMP_WORDS[COMP_CWORD]}) )
}
complete -F _project project
When you open a new tab, it will automatically cd into your most recently changed project. To switch projects, just do project existing-project (with tab completion on the project name). To see a list of projects, project list and to return to the currently active project, just project from anywhere.
Hope that brings someone else as much utility as it has me.