1

我想添加到我的 .zshrc 函数中,该函数将对具有“.c”后缀的文件执行操作。例如,

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

当我输入“foo.c”时必须执行“gcc foo.c -o foo”

但是当我将此函数添加到“.zshrc”时,我的 shell 在登录时开始打印“找不到匹配项:*.c”。

我可以以其他方式执行此操作或使此功能“懒惰”吗?

4

1 回答 1

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'

将按预期工作。

于 2011-04-14T16:57:59.890 回答