使用模板启动扩展名为.c的任何Emacs缓冲区

vin*_*456 15 emacs templates

我写了很多短暂的一次性程序,我发现自己反复做的事情之一就是输入代码

#include <stdio.h>
#include <stdlib.h>

int main(void){

}
Run Code Online (Sandbox Code Playgroud)

为了节省一些肌腱命中,我想知道是否有可能在我创建扩展名为.c的缓冲区时插入一个简单的模板.

Tom*_*ham 18

把这样的东西放进去 .emacs

(define-skeleton c-throwaway
  "Throwaway C skeleton"
  nil
  "#include <stdio.h>\n"
  "#include <stdlib.h>\n"
  "\n"
  "int main(void){\n"
  "\n"
  "}\n")
Run Code Online (Sandbox Code Playgroud)

和eval(C-x C-e)吧.这将为您提供一个c-throwaway插入模板的函数().

要自动插入,您需要激活 auto-insert-mode.一旦你这样做,你可以describe-variable auto-mode-alist阅读emacs如何处理它的一些开放文件魔法.然后定义auto-insert-alist在找到新文件时应用它.

也许这样的事情

(define-auto-insert "\\.\\([Cc]\\|cc\\|cpp\\)\\'" 'c-throwaway)
Run Code Online (Sandbox Code Playgroud)

更多详情:


jro*_*way 10

我使用http://emacs-template.sourceforge.net/上的 template.el

基本上,我创建一个名为〜/ .templates/TEMPLATE.c的文件,然后将其插入到我的.c文件中.如果您不想将文本转储到缓冲区,也可以使用特殊标记和任意lisp表达式.我使用此功能,以便当它们命名为lib/Foo/Bar.pm时,Perl模块以"package Foo :: Bar"开头.非常便利.