Jumping jack tab
15:07
I use the console a lot. I also like to patch together small scripts to make my shell experience smoother and more efficient. (Read: indulge my laziness.) Inspired by a talk by Paul Irish where he introduces his selection of tools – specifically a command for quickly jumping into a project folder, which I naturally immediately adopted. Just to show off, I also included tab-completion.
If you feel you could use it, snag the source code below (and party on!), or download it from the gist.
Setting up is pretty straight forward and is “documented” in the comments at the top. If you don’t like
<code>#!bash
# Usage:
#
# In your .bashrc
#
# export PROJECTS_HOME=<path_to_projects_root>
# source jump-to-project.bash
#
# In you shell:
#
# $ z <project name>[ENTER]
#
# Use [TAB] for auto-completion, double tap for a list.
#
# Optionals:
#
# export PROJECTS_SUFFIX=<optional suffix to strip>
#
# For prettier tab-completion, fill PROJECTS_SUFFIX with a
# value you want to strip from folder names. I use '.dev.local'
#
# export PROJECTS_TALKATIVE=1
#
# For some feedback when switching into a project folder,
# fill PROJECTS_TALKATIVE with any value.
if [ "x$PROJECTS_HOME" = "x" ]; then
echo "PROJECTS_HOME not set"
return
fi
_find_all_projects () {
if [ "x$1" = "x" ];
then
find $PROJECTS_HOME -maxdepth 1|xargs basename -s "$PROJECTS_SUFFIX"
else
find $PROJECTS_HOME -maxdepth 1 -name "$1*"|xargs basename -s "$PROJECTS_SUFFIX"
fi
return 0
}
_find_project_path () {
find $PROJECTS_HOME -maxdepth 1 -name "$1*"
}
_find_project () {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(_find_all_projects)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
z () {
if [ "x$HOME" = "x" ]; then
echo "\$HOME not set"
return 1
fi
MATCHES=($(_find_all_projects $1))
if [ "x$1" = "x" ]; then
echo "Usage: zz <project>"
echo "Projects:"
echo ${MATCHES[@]}
return 0
fi
if [ ${#MATCHES[@]} -eq 0 ]; then
echo "No match for $1"
return 0
fi
if [ ${#MATCHES[@]} -gt 1 ];
then
echo "Ambiguous match '$1': ${MATCHES[@]}"
fi
cd $(_find_project_path ${MATCHES[0]}) && [ ! -z "$PROJECTS_TALKATIVE" ] && echo "Project ${MATCHES[0]} in $(pwd)"
return 0
}
complete -F _find_project z</code>