まず, 他のシェルをお使いの人にはごめんなさい.
aliasコマンドの-sが使えるのは zsh 4.2.x- で, 他のシェルでは実装されていないと思います.
あと, もう既に alias -s 知ってるよーって人は読まなくていいです.
alias -s って何?
打ったコマンドの後ろ(suffix)を見て, 適当に宜しくやってくれるやつです.
次の例を見て下さい.
~$ echo "print ('hello, world')" > hello.py ~$ python hello.py hello, world ~$ alias -s py=python py=python ~$ ./hello.py hello, world ~$
Pythonのコードは python hello.py で実行出来ますが, お尻が py なら python を使います, とaliasしておけば, ./hello.py で実行できるのです.
Haskell使うなら
alias -s hs=runhaskell
としておきましょう.
~$ echo 'main = putStrLn "hello, world"' > hello.hs ~$ ./hello.hs hello, world ~$
で? 何が便利なの?
え, やだーー便利じゃないですかーー!!!
例えば,
if [ `uname` = "Darwin" ]; then alias eog='open -a Preview' fi alias -s {png,jpg,bmp,PNG,JPG,BMP}=eog
としておけば,
~$ ./test.jpg
~$
で, 画像が開けるんですよ!
(eogはUbuntuの画像ビューアーです)
更に,
alias -s mp3=mplayer
としておけば,
~$ ./test.mp3
で, 音楽が聞けるんですよ!
インパクトが足りない
え, そうですか.
if [ `uname` = "Darwin" ]; then alias google-chrome='open -a Google\ Chrome' fi alias chrome='google-chrome' alias -s html=chrome
って設定しておいて,
~$ ./index.html ~$
で, Google Chromeが起動するんですよ!
素晴らしい!
もっとすごいのが欲しいのぉ
欲張りですね.
function extract() { case $1 in *.tar.gz|*.tgz) tar xzvf $1;; *.tar.xz) tar Jxvf $1;; *.zip) unzip $1;; *.lzh) lha e $1;; *.tar.bz2|*.tbz) tar xjvf $1;; *.tar.Z) tar zxvf $1;; *.gz) gzip -d $1;; *.bz2) bzip2 -dc $1;; *.Z) uncompress $1;; *.tar) tar xvf $1;; *.arj) unarj $1;; esac } alias -s {gz,tgz,zip,lzh,bz2,tbz,Z,tar,arj,xz}=extract
としておけば, あらゆる圧縮ファイルを
~$ ./test.zip ~$ ./test.tar.gz
みたいに, 展開出来ます! *1
もうこれでオプション覚える必要が無くなりますね!
コンパイルしなきゃいけないんだけど
はい! はい!
function runcpp () { g++ -O2 $1; ./a.out } alias -s {c,cpp}=runcpp
こうしておけば,
~$ echo '#include <stdio.h>\nmain () { printf("hello, world\\n"); }' > test.c ~$ ./test.c hello, world ~$
ってできちゃうんです!
え, ファイルに引数渡したい?
それでもだいじょーぶ!
~$ function runcpp () { g++ -O2 $1; ./a.out $2 $3 $4 $5 $6 $7 $8 $9; } ~$ echo '#include <stdio.h>\nmain(int c, char *v[]){int i; for (i=1; i<c; i++) printf("%s\\n", v[i]); }' > test.c ~$ cat test.c #include <stdio.h> main(int c, char *v[]){int i; for (i=1; i<c; i++) printf("%s\n", v[i]); } ~$ ./test.c -a -b someoption zsh is the supreme shell -a -b someoption zsh is the supreme shell ~$
完璧っ☆
まとめ
alias -s は神である.
setopt auto_cd と親和性が高く, 似たような感じでファイルとディレクトリーを処理できる.
Cのソースを./test.cで実行するの, やみつきになる.
じゃあ alias -s の設定, いつやるのか?
追記(2013/03/01)
コメントで, shiftを使う方法を教えて頂きました.
function runcpp () { g++ $1 && shift && ./a.out $@ }
こうすれば引数を10個以上取れますね.
こうさん, ありがとうございます.