2011-04-14 82 views
1

我想添加到我的.zshrc函數中,該函數將對具有「.c」後綴的文件執行操作。例如,「* .foo」shell函數

*.c() { 
    gcc $0 -o ${0%.*} 
} 

必須執行「文件foo.c的gcc -o foo」的,當我進入「foo.c的」

但是,當我添加了這個功能,「.zshrc」,我的殼開始打印「登錄時找不到匹配項:* .c」。

我可以用其他方式做這個或者使這個函數「懶」嗎?

回答

3

您需要alias -s行爲。從手冊頁:

ALIASES 
    Suffix aliases are supported in zsh since version 4.2.0. Some examples: 

     alias -s tex=vim 
     alias -s html=w3m 
     alias -s org=w3m 

    Now pressing return-key after entering foobar.tex starts vim with foobar.tex. Calling 
    a html-file runs browser w3m. www.zsh.org and pressing enter starts w3m with argument 
    www.zsh.org. 

將您的書面功能結合到後綴別名,你應該設置去!

首先,在寫這份表單功能:

compile_c() {  
    gcc $1 -o ${1%.*} 
} 

然後後綴別名預期

alias -s c='compile_c' 

會工作。

+0

它不工作,因爲$ 0,$ 1,$ 2變量沒有在別名中定義 – user123980801 2011-04-14 17:04:30

+0

@ user123980801我認爲這是progo的意思:'__gcc_compile(){gcc $ 1 -o $ {1%。*}};別名-s c = __ gcc_compile' - 這適用於zsh 4.3.11。 – yibe 2011-04-14 17:36:57

+0

是的,它的作品,看我的例子。 – progo 2011-04-14 17:37:43