“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