Snippets 代码片段

Snippet(片段)在编程中是指一段可复用的代码或文本,在绝大部分编辑器和IDE中都有Code Snippet功能。Code Snippet代码片段自动生成据说是textMate首创的,现在主流的编辑器和IDE都有类似的功能,可以让用户针对不同的场景定制、管理和插入代码片段。在Vim中自从我用了ultisnips,傍观者的智商再也跟不上我码代码的速度,谈笑风生,弹指间满屏代码,可谓居家旅行必备装X神奇。

ultisnips演示

Vim中的Ultisnips插件

Ultisnips是Vim的明星插件,可以配合YouCompleteMe或者Neocomplete的自动补全,使用体验上不逊于任何现代编译器和IDE。使用Vundle安装ultisnips非常的简单,设置也有非常详细的文档

ultisnips 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
" Track the engine.
Plugin 'SirVer/ultisnips'
" Snippets are separated from the engine. Add this if you want them:
Plugin 'honza/vim-snippets'
" Trigger configuration. Do not use <tab> if you use https://github.com/Valloric/YouCompleteMe.
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
" If you want :UltiSnipsEdit to split your window.
let g:UltiSnipsEditSplit="vertical"

当然vim大神们肯定不会只造一个轮子,snipmateneosnippet也都不少拥护者,还有一些不错的snippet库:123。这些插件大部分都支持textmate格式的snippet,配置和trigger方法都大同小异,并可以根据自己项目的code guideline修改snippets。但是像所有Vim的东西一样,入门的过程并不轻松,暗坑无数。本着适度折腾的原则,问题遇见了再说。

cpp.snippetslink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
extends c
###########################################################################
# TextMate Snippets #
###########################################################################
snippet cl "class .. (class)" !
class ${1:`!p snip.rv = snip.basename or "name"`}
{
public:
${1/(\w+).*/$1/}(${2:arguments});
virtual ~${1/(\w+).*/$1/}();
private:
${0:/* data */}
};
endsnippet
snippet ns "namespace .. (namespace)" !
namespace${1/.+/ /m}${1:`!p snip.rv = snip.basename or "name"`}
{
${VISUAL}${0:${VISUAL/(.*)/(?1::\/* code *\/)/}}
}${1/.+/ \/\/ namespace /m}$1${1/.+/ /m}
endsnippet

Sublime text 3中的snippet

Sublime Text作为更现代更人性化的编辑器,snippet的配置也更加方便,只要在Tools工具栏中点击New Snippet就能创建一个新的Snippet模板,这个模板的语法非常直观,用户可以定义snippet的tabTrigger和scope。

codeblock.sublime-snippet
1
2
3
4
5
6
7
8
9
10
11
<snippet>
<content><![CDATA[
{% codeblock lang:${1:lang} %}
${2:code}
{% endcodeblock %}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>codeblock</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>text.html.markdown</scope>
</snippet>

这里需要注意的是,截止目前为止,sublime的每个snippet需要保存在单独文件中,而且文件的后缀名一定要是.sublime-snippet。sublime中的scope概念是从textmate中借鉴过来的,下面的列表是sublime中主要支持的scope定义。

scopes.txtlink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Here is a list of scopes to use in Sublime Text snippets -
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