我们使用sublime编辑器,很多时候需要多次复用同样一段代码,但是又不想重复输入,那么可以使用sublime的代码块(snippet)功能。
一、操作:Tools --> Developers --> New snippet
如下图:
那么就可以进入sublime预设的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>
二、我们可以将需要复用的代码段覆盖掉原先的 Hello, ${1:this} is a ${2:snippet}. 也就是说CDATA[ ] 括号中放我们自己的代码段
<snippet>
<content><![CDATA[
# -*- coding:utf-8 -*-
# Coder: Yao Zhao
# Github: https://github.com/yyyzzzhao
# ==============================================================================
]]></content>
三、在这之后需要设置代码段触发词,以及代码片段的作用范围。
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>title</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.python</scope>
这里我设置了 title作为代码段呼出指令,同时指定作用范围为python代码.
四、保存
Ctrl + s 保存,保存文件的后缀为 .sublime-snippet
这样在自己需要插入代码段的时候直接 触发词 + Tab 就能呼出预存代码段啦!