个人环境:
win7 64位专业版 + Sublime Text3(以下简称ST)
snippet可以理解为代码片段, 在自定义好snippet后就可以快带生成相应的代码片段.
先来感受下效果:
如何编写自己的snippet呢?
在ST中: 依次选择tools -> Developer -> New Snippet
, 就打开默认的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>
其中的content
中的CDATA
就是实际的snippet的内容.
tabTrigger
就是触发snippet的字符串, 比如上图中的html5
.
scope
就是说在哪里会触发, 可以简单理解为文件类型.
下面是上图中的snippet写法:
<snippet>
<content><![CDATA[
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<meta name="format-detection" content="email=no">
<title>${1}</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
${2}
</body>
</html>
]]></content>
<tabTrigger>html5</tabTrigger>
<scope>text.html</scope>
</snippet>
可以看到上面有个${1}
, ${2}
, 很容易猜到, 这就是按tab键时,光标依次停留的位置.
注意事项:
- 保存snippet时, 文件扩展名要是
sublime-snippet
.
参考:
- http://sublimetext.info/docs/en/extensibility/snippets.html
- 利用snippet生成css box-shadow
欢迎补充指正!