Snippets是可以復(fù)用的代碼片段模板瘦陈,可以幫助你快速輸入大量重復(fù)內(nèi)容锋喜,極大提高寫作效率质蕉。
在Sublime Text中新建Snippet蔓同,選擇菜單:Tools->Developer->New Snippet...,Sublime Text會生成一個Snippet模板乾戏。
<snippet>
<content><![CDATA[
Hello, ${1:this} is a ${2:snippet}.
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<!-- <tabTrigger>hello</tabTrigger> -->
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>
將模板內(nèi)容修改如下:
<snippet>
<content><![CDATA[
Hello, this is a snippet.
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>hello</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.python</scope>
</snippet>
簡單介紹一下各節(jié)點含義(看不懂沒關(guān)系迂苛,后面的實際操作會讓你豁然開朗,可以回頭再看^_^
):
-
content
節(jié)點內(nèi)容表示實際的代碼片段鼓择。 -
tabTrigger
節(jié)點內(nèi)容表示用來輸入代碼片段的快捷字符串三幻。 -
scope
節(jié)點內(nèi)容表示代碼片段會在哪種上下文環(huán)境下激活, 比如上面代碼定義了source.python, 意思是這段代碼片段會在python語言環(huán)境下激活。注意:Scope不是文件擴展名呐能,可以從菜單:Tools->Developer->Show Scope Name查看當前文件擴展名對應(yīng)的Scope念搬。
把修改后的模板保存為文件hello.sublime-snippet
(保存目錄保持默認打開的目錄即可)。
新建文件t.py
摆出,在文件中輸入hello
朗徊,按下Tab
鍵,則輸出如下:
好了偎漫,這就是Snippet最基本的用法爷恳,是不是很簡單很強大!更多高級功能請參考官方文檔http://docs.sublimetext.info/en/latest/extensibility/snippets.html
一個常見問題:針對Markdown文件的Snippet沒有出現(xiàn)在自動補全列表中
解決辦法是選擇菜單Preferences->Settings象踊,在右側(cè)的窗口中增加如下設(shè)置:
"auto_complete_selector": "meta.tag - punctuation.definition.tag.begin, source - comment - string.quoted.double.block - string.quoted.single.block - string.unquoted.heredoc, text",
附常見語言對應(yīng)Scope值:
ActionScript: source.actionscript.2
AppleScript: source.applescript
ASP: source.asp
Batch FIle: source.dosbatch
C#: source.cs
C++: source.c++
Clojure: source.clojure
CoffeeScript: source.coffee
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
LESS: source.css.less
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
SASS: source.sass
Scala: source.scala
Shell Script: source.shell
SQL: source.sql
Stylus: source.stylus
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