YASnippet是Emacs的一个模版系统.允许输入一个缩写,自动为你扩展为相应的功能模版。捆绑的语言模版包括 C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML, CSS 等等。代码段的语法灵感源于 TextMate,你甚至可以导入大部分的 TextMate模版。

Yasnippet 已经迁移到GitHub上了:http://github.com/capitaomorte/yasnippet!!!

google code上的老版本在这里:

http://code.google.com/p/yasnippet/downloads/list

还有个Demo视频:

http://code.google.com/p/yasnippet/downloads/detail?name=yas_demo.mp4

安装

There are two archives you can download. To quickly tryout YASnippet, download the simpler "bundle" version. If you plan to modify the bundled templates and/or build your own, download the "normal" package.

有2个归档供你下载。如果想快速体验下YASnippet,可以只需下载"bundle"版。如果你打算修改打包的模版或者创建你自己的模版,那么要下载"normal"包了。

使用yasnippet-bundle.el安装

  1. Download the latest yasnippet-bundle-x.y.z.el.tgzand unpack it.
  2. You'll get a file named yasnippet-bundle.el, put it under~/.emacs.d/plugins/ (create the directory if not exists).
  3. Open the file in Emacs, and type Alt+x eval-buffer.


 
  
  1. 下载最新版本的yasnippet-bundle-版本号.el.tgz并解压缩。 
  2.  
  3. 会得到一个名字为yasnippet-bundle.el的文件把他放到~/.emacs.d/plugins/目录下(如果不存在,自行创建). 
  4.  
  5. 在Emacs里打开该文件,然后Alt-x eval-buffer命令。 

OK,现在打开任意一种语言的源文件,就会看到菜单栏出现一个YASnippet的菜单。

当然你可以使用菜单项执行模版扩展替换,也可以使用触发键并按TAB扩展。

为了让Emacs启动时能自动加载YASnippet,需要在你的~/.emacs配置文件里加上这段:

 
  
  1. (add-to-list 'load-path 
  2.               "~/.emacs.d/plugins"
  3. (require 'yasnippet-bundle) 

正常安装

为了使用正常的包安装YASnippet,下载解压最新的yasnippet-x.y.z.tar.bz2,得到一个名字类似yasnippet-x.y.z的目录,你可以把它放在 ~/.emacs.d/plugins目录中,

在.emacs文件中添加:

 
  
  1. (add-to-list 'load-path 
  2.               "~/.emacs.d/plugins/yasnippet-x.y.z") 
  3. (require 'yasnippet) ;; not yasnippet-bundle 
  4. (yas/initialize) 
  5. (yas/load-directory "~/.emacs.d/plugins/yasnippet-x.y.z/snippets") 

完整的定制参照文档。

如何使用YASnippet

自从0.6版本以来,YASnippet提供更多的功能。当然你没必要统统搞会每一个,可是了解他们会大大提升你的体验。

组织代码段

扩展代码段

书写代码段

使用YASnippet菜单