Skip to content

Instantly share code, notes, and snippets.

@chaoxu
Created March 12, 2011 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaoxu/867382 to your computer and use it in GitHub Desktop.
Save chaoxu/867382 to your computer and use it in GitHub Desktop.
Open a file by association. -o to open -e to edit.
#!/bin/bash
#long file extension usually means it's a text
SELF="$(cd ${0%/*} && echo $PWD/${0##*/})"
if [[ $1 == "-o" ]]; then
a="$HOME/.association"
text="vim -u /usr/share/vim/vim73/macros/less.vim"
default="xdg-open"
elif [[ $1 == "-e" ]]; then
a="$HOME/.associationedit"
text="vim"
default="$SELF -o"
fi
f=$2
ext=`basename "$2" | awk -F"." '{ print $NF }'`
regexurl='^(http|https|ftp)\://'
if [[ $2 =~ $regexurl ]]; then
f=`mktemp`
mv $f $f.$ext
f="$f.$ext"
wget -q -O $f "$2"
fi
p=`cat $a | grep -iE -B 1 "(^$ext | $ext | $ext$|^$ext$)" | head -n 1`
l=`echo -e "$p\c" | wc -m`
if [[ $l -gt 0 ]]; then
$p "$f"
else
l=`echo -e "$ext\c" | wc -m`
if [[ $l -gt 4 ]]; then
$text "$f"
else
$default "$f"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment