最近写前端总觉得pycharm比较重,开始尝试sublime。今天写blog的时候想到试一下snippet功能。正好在写blog的时候就用上了。
创建方法:Tools > New 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>
解释下:
自定义代码片段:
Hello, ${1:this} is a ${2:snippet}.
${1} 这个是光标的停留地方,
${1:this}this 是参数可选 如果有多个的话可以继续
${1} ${2}`
触发设置:
<tabTrigger>hello</tabTrigger>
在文档中输入hello 按tab就会弹出前面定义的代码块
<scope>source.python</scope>
指定在什么文件中,or文件类型中触发
<description>My Fancy Snippet</description>
描述在菜单中显示,不定义的话只显示文件名
例子:以博客的头信息为例
<snippet> <content><![CDATA[ Title: ${1} Ddate: ${2} Category: ${3} Tags: ${4} Author: ${5} ]]></content> <tabTrigger>blogmd</tabTrigger> <!-- 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>
写好后保存在:~/Library/Application\ Support/Sublime\ Text\ 3/Packages/User/snippet/blog.sublime-snippet
注意后缀必须是: .sublime-snippet
snippet 目录是自己创建的,为了归类保存自定义的代码片段
这下新建一个文档 输入 blogmd 按tab就会弹出前面定义的代码块了。