Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active August 29, 2015 14:17
Show Gist options
  • Save davidhq/2729b32e644ff72a330d to your computer and use it in GitHub Desktop.
Save davidhq/2729b32e644ff72a330d to your computer and use it in GitHub Desktop.
cd_projects
# p proj -> cd ~/Projects/project
# works best (fastest) if your projects are lower-case and you refer to them as such
# if not, then for best performance (no lag) you have to call "p Proj" (if ~/Projects/Project exists)
function p {
cd ~/Projects
local match
if [ -n "$1" ]; then
local min_size=1000
# first try only directories that start exactly with our input
for d in $1*/ ; do
# if there were no matches then strangely we get this and we have to ignore it
if [ $d != "$1*/" ]; then
if [[ $d == $1* ]]; then
local size=${#d}
if [[ $size -lt $min_size ]]; then
match=$d
min_size=$size
fi
fi
fi
done
if [ -z "$match" ]; then
local proj=`echo $1 | tr '[:upper:]' '[:lower:]'`
local min_size=1000
# search all directories canse-insensitive now
for d in */ ; do
local dir=`echo $d | tr '[:upper:]' '[:lower:]'`
if [[ $dir == $proj* ]]; then
local size=${#d}
if [[ $size -lt $min_size ]]; then
match=$d
min_size=$size
fi
fi
# if exact match stop the loop to gain some performance
if [[ $dir == $proj/ ]]; then
cd $d
unset match
break
fi
done
fi
fi
if [ -n "$match" ]; then
cd $match
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment