Created
December 20, 2012 11:58
-
-
Save nikcorg/4344901 to your computer and use it in GitHub Desktop.
Shell assistant for navigating between project dirs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment