Skip to content

Instantly share code, notes, and snippets.

@stepancheg
Created August 16, 2012 00:40
Show Gist options
  • Save stepancheg/3365062 to your computer and use it in GitHub Desktop.
Save stepancheg/3365062 to your computer and use it in GitHub Desktop.
# tested in zsh
# Don't try this at home.
command_not_found_handler() {
replaced=false
set -A args "$@"
# aliases like gcc-O2='gcc -O2'
if [ $args[1] =~ "^([^-]+)(-.+)$" ]; then
shift args
set -A args $match[1] $match[2] $args
replaced=true
fi
# aliases like find.='find .'
if [ $args[1] =~ "^(.*[^.])(\.+)$" ]; then
cmd=$match[1]
arg=$match[2]
if [ $arg =~ "^(\.|\.\.)$" ]; then
dir=$arg
else
dir=".."
for i in {3..${#arg}}; do
dir="$dir/.."
done
fi
shift args
set -A args $cmd $dir $args
replaced=true
fi
# aliases like stglog4f='stg log -n 4 -f'
if [ $args[1] =~ "^(stg|git|hg|svn)log([0-9]+)(.*)$" ]; then
vcs=$match[1]
case $vcs in
stg|git) opt=-n ;;
*) opt=-l ;;
esac
shift args
if [ -z $match[3] ]; then
set -A args $vcs log $opt $match[2] $args
else
set -A args $vcs log $opt $match[2] -$match[3] $args
fi
replaced=true
fi
# aliases like gitst='git st'
if [ $args[1] =~ "^(hg|git|stg|svn|brew|jar|tar|sudo)(.+)" ]; then
shift args
set -A args $match[1] $match[2] $args
replaced=true
fi
# aliases like egreprw='egrep -rw'
if [ $args[1] =~ "^(ls|z?(e|f)?grep|du|df|wc|make|last|netstat)(.+)$" ]; then
shift args
set -A args $match[1] -$match[3] $args
replaced=true
fi
# we are in subprocess here, so `cd` and other builtins do not work
if [ $args[1] =~ "^(cd|set)$" ]; then
return 1
fi
# do not call exec if nothing is changed to avoid endless recursion
if $replaced; then
#echo $args >&2
exec $args
fi
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment