Movatterモバイル変換


[0]ホーム

URL:


プログラムモグモグ

この広告は、90日以上更新していないブログに表示しています。

zshのalias -s (suffix alias)が神な件

まず, 他のシェルをお使いの人にはごめんなさい.
aliasコマンドの-sが使えるのはzsh 4.2.x- で, 他のシェルでは実装されていないと思います.
あと, もう既に alias -s 知ってるよーって人は読まなくていいです.

alias -s って何?

打ったコマンドの後ろ(suffix)を見て, 適当に宜しくやってくれるやつです.
次の例を見て下さい.

~$echo"print ('hello, world')"> hello.py~$ python hello.py hello, world~$alias-spy=pythonpy=python~$ ./hello.py hello, world~$

Pythonのコードはpython hello.py で実行出来ますが, お尻が py ならpython を使います, とaliasしておけば, ./hello.py で実行できるのです.


Haskell使うなら

alias-shs=runhaskell

としておきましょう.

~$echo'main = putStrLn "hello, world"'> hello.hs~$ ./hello.hs                                      hello, world~$

で? 何が便利なの?

え, やだーー便利じゃないですかーー!!!
例えば,

if [`uname`="Darwin"];thenaliaseog='open -a Preview'fialias-s{png,jpg,bmp,PNG,JPG,BMP}=eog

としておけば,

~$ ./test.jpg~$

で, 画像が開けるんですよ!
(eogはUbuntuの画像ビューアーです)


更に,

alias-smp3=mplayer

としておけば,

~$ ./test.mp3

で, 音楽が聞けるんですよ!

インパクトが足りない

え, そうですか.

if [`uname`="Darwin"];thenaliasgoogle-chrome='open -a Google\ Chrome'fialiaschrome='google-chrome'alias-shtml=chrome

って設定しておいて,

~$ ./index.html~$

で,Google Chromeが起動するんですよ!
素晴らしい!

もっとすごいのが欲しいのぉ

欲張りですね.

functionextract() {case$1in    *.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
もうこれでオプション覚える必要が無くなりますね!

コンパイルしなきゃいけないんだけど

はい! はい!

functionruncpp () { 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~$

ってできちゃうんです!


え, ファイルに引数渡したい?
それでもだいじょーぶ!

~$functionruncpp () { 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 ~$ cattest.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-bsomeoptionzshisthesupremeshell~$

完璧っ☆

まとめ

alias -s は神である.
setopt auto_cd と親和性が高く, 似たような感じでファイルとディレクトリーを処理できる.
Cのソースを./test.cで実行するの, やみつきになる.

じゃあ alias -s の設定, いつやるのか?

追記(2013/03/01)

コメントで, shiftを使う方法を教えて頂きました.

functionruncpp () { g++$1&&shift&& ./a.out$@}

こうすれば引数を10個以上取れますね.
こうさん, ありがとうございます.

検索

プログラムモグモグのはてなブックマーク総数

Supported By Haskell-jp.

引用をストックしました

引用するにはまずログインしてください

引用をストックできませんでした。再度お試しください

限定公開記事のため引用できません。

読者です読者をやめる読者になる読者になる

[8]ページ先頭

©2009-2025 Movatter.jp