为sublime text2定义snippet
“snippet”在英语里面是“片段”的意思。当我们编码时候,通常想要打几个简略的字符串,就出来一些固定的模板,这个就类似于前端的zen coding。
不过,目前,sublime text2 的zen coding 不支持了(zen coding改名字了 ,改为了emmet,所以以前安装的zen coding 插件不能用的),只能实现一些简单的类似zen coding的功能。
定义很简单,先通过tools->new snippets就进入了新建文件。
文件包括以下内容:
<snippet> <content><![CDATA[ ]]></content> <!-- Optional: Set a tabTrigger to define how to trigger the snippet --> <tabTrigger></tabTrigger> <!-- Optional: Set a scope to limit where the snippet will trigger --> <scope></scope> <!-- Optional: Description to show in the menu --> <description>My Fancy Snippet</description> </snippet>
现在对以上的参数进行说明:
- content:是你要定义的模板内容
- tabTrigger:是你按tab键之前需要输入的快捷字符串,输了该字符串后,按tab键,就能生成你需要的模板内容啦
- scope:是对文件格式的限制,比如,html格式的要设置为text.html
- description:说明
这里给个示例,我做的html5文档声明,code如下:
<snippet> <content><![CDATA[ <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html> ]]></content> <!-- Optional: Set a tabTrigger to define how to trigger the snippet --> <tabTrigger>html5</tabTrigger> <!-- Optional: Set a scope to limit where the snippet will trigger --> <scope>text.html</scope> </snippet>
然后,保存文件为.sublime-snippet格式,重启sublime text2,新建html文件,输入html5,按tab键,就能显示出如下代码了:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html>
这里需要注意的是scope,因为你不知道如何设置,才会让sinppet正常响应。设置出错,自然是无效啦。
列出scope列表,参考自:https://gist.github.com/iambibhas/4705378:
ActionScript: source.actionscript.2 AppleScript: source.applescript ASP: source.asp Batch FIle: source.dosbatch C#: source.cs C++: source.c++ Clojure: source.clojure CSS: source.css D: source.d Diff: source.diff Erlang: source.erlang Go: source.go GraphViz: source.dot Groovy: source.groovy Haskell: source.haskell HTML: text.html(.basic) JSP: text.html.jsp Java: source.java Java Properties: source.java-props Java Doc: text.html.javadoc JSON: source.json Javascript: source.js BibTex: source.bibtex Latex Log: text.log.latex Latex Memoir: text.tex.latex.memoir Latex: text.tex.latex TeX: text.tex Lisp: source.lisp Lua: source.lua MakeFile: source.makefile Markdown: text.html.markdown Multi Markdown: text.html.markdown.multimarkdown Matlab: source.matlab Objective-C: source.objc Objective-C++: source.objc++ OCaml campl4: source.camlp4.ocaml OCaml: source.ocaml OCamllex: source.ocamllex Perl: source.perl PHP: source.php Regular Expression(python): source.regexp.python Python: source.python R Console: source.r-console R: source.r Ruby on Rails: source.ruby.rails Ruby HAML: text.haml SQL(Ruby): source.sql.ruby Regular Expression: source.regexp RestructuredText: text.restructuredtext Ruby: source.ruby Scala: source.scala Shell Script: source.shell SQL: source.sql TCL: source.tcl HTML(TCL): text.html.tcl Plain text: text.plain Textile: text.html.textile XML: text.xml XSL: text.xml.xsl YAML: source.yaml