在Sublime Text 3中添加snippet

1,点击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>

参数讲解:

  [CDATA['这里是你要现实的内容']]

    你所需要显示的内容都要写在CDATA[]中

  ${1:text}

    这里的数字1,代表光标第一显示的位置,必选,你还可以设置更多,${2:text}光标第二显示的位置,按tab键由1位置到2位置,${100:text}光标第100显示的位置

    text代表默认替代文字,可选

      

     

  <tabTrigger>hello</tabTrigger>
这里的hello代表触发的表达式,也就是当你输入hello会触发这个snippet,记得要解除这句话的注释,删掉<!-- -->
  
  <scope>source.python</scope>

    这里的.python代表触发的文件格式,也就是只有你在编辑python文件时才会起作用,同理可以添加<scope>source.python.php</scope>等等,我通常会注释掉这句话
 

下面举个简单例子

<snippet>
    <content><![CDATA[console.log("${1}");]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>con</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

这是我写的 一个用来快速添加console.log()的snippet,当我输入con的时候,看到提示列表后,按enter   (这里的hello后面讲)

我们看最关键的一行

<content><![CDATA[console.log("${1}");]]></content>

这里的

console.log("${1}");

就是我要插入显示的内容,${1},意味着光标第一个所在的位置,效果就是当整个内容查到你写的文档中的时候,光标默认在两个引号中间

如果这里我写成${1:text},就表示光标替代处的内容是text,默认text是选中状态

console.log("text");

效果如图

还可以添加更多的${n:text}按tab键切换位置

例如

<content><![CDATA[console.log("${1:text}");${20:tttt}]]></content>

 效果是

单击一次tab之后

最后,但文件编辑完成 我们保存为hello.sublime-snippet,这就是前面的hello的原因

保存路径是C:UserszhangsanAppDataRoamingSublime Text 3PackagesUser

zhangsan是你的电脑用户名

如果你写成console.log().sublime-snippet,就是这样

ok 打完收工

原文地址:https://www.cnblogs.com/geqianst/p/3689425.html