欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Sublime Text自定义snippet

程序员文章站 2024-03-23 20:58:04
...

个人环境:
win7 64位专业版 + Sublime Text3(以下简称ST)

snippet可以理解为代码片段, 在自定义好snippet后就可以快带生成相应的代码片段.

先来感受下效果:

Sublime Text自定义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.

参考:

欢迎补充指正!

相关标签: sublime-text