Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active August 29, 2015 14:23
Show Gist options
  • Save davidhq/cc7caf0e7e784eee3810 to your computer and use it in GitHub Desktop.
Save davidhq/cc7caf0e7e784eee3810 to your computer and use it in GitHub Desktop.
# swiss knife
function c {
local dir
dir=`pwd`
if [ -z "$1" ]; then
cd - > /dev/null
else
if [ -d "$1" ]; then
cd "$1"
elif [ -f "$1" ]; then
if [ -n "$2" ]; then
cat "$1" | grep "$2" -i
else
cat "$1"
fi
else
awesome_cd . $1
if [ "$dir" == "`pwd`" ]; then
echo "Creating..."
ccd "$1"
fi
fi
fi
}
ccd () { mkdir -p "$@" && cd "$@"; }
alias ..='cd ..'
alias ...='cd ../..'
function awesome_cd {
local base
base=$1
cd $base
shift
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