代码片段设置
Sublime 菜单栏->Tools→New Snippet→输入以下内容:
<snippet>
<content><![CDATA[
<?php
// +----------------------------------------------------------------------+
// | The CompanyName Inc |
// +----------------------------------------------------------------------+
// | Copyright (c) 2017, CompanyName Inc. All rights reserved. |
// +----------------------------------------------------------------------+
// | Authors: XXX, CompanyName Inc. |
// | |
// +----------------------------------------------------------------------+
/**
* @version 1.0
* @author xxx
* @date
*/
]]></content>
<tabTrigger>phpheader</tabTrigger>
<description>phpheader</description>
</snippet>
然后保存文件名为phpheader.sublime-snippet,保存到指定路径。
详细教程:
1. Snippe创建,存储和格式
Snippet可以存储在任何的文件夹中, 并且以.sublime-snippet为文件扩展名, 默认是存储在.sublime-snippet文件夹下.
Snippet文件是以.sublime-snippet为扩展的XML文件, 可以命名为XXX.sublime-snippet, 创建自己的snippet的方式为菜单栏Tools | New Snippet..
新建的文件格式:
<snippet>
<content><![CDATA[Type your snippet here]]></content>
<!-- Optional: Tab trigger to activate the snippet -->
<tabTrigger>hello</tabTrigger>
<!-- Optional: Scope the tab trigger will be active in -->
<scope>source.python</scope>
<!-- Optional: Description to show in the menu -->
<description>My Fancy Snippet</description>
</snippet>
四个组成部分:
- content:其中必须包含,否则无法工作, Type your snippet here用来写你自己的代码片段
- tabTrigger:用来引发代码片段的字符或者字符串, 比如在以上例子上, 在编辑窗口输入hello然后按下tab就会在编辑器输出Type - your snippet here这段代码片段
- scope: 表示你的代码片段会在那种语言环境下激活, 比如上面代码定义了source.python, - 意思是这段代码片段会在python语言环境下激活.
- description :展示代码片段的描述, 如果不写的话, 默认使用代码片段的文件名作为描述
2. snippet环境变量
环境变量名 描述
$TM_FILENAME 用户文件名
$TM_FILEPATH 用户文件全路径
$TM_FULLNAME 用户的用户名
$TM_LINE_INDEX 插入多少列, 默认为0
$TM_LINE_NUMBER 一个snippet插入多少行
$TM_SOFT_TABS 如果设置translate_tabs_to_spaces : true 则为Yes
$TM_TAB_SIZE 每个Tab包含几个空格
3. snippet Fields
设置Fields, 可以通过tab键循环的改变代码片段的一些值
<snippet>
<content><![CDATA[
=================================
First Name: $1
Second Name: $2
Address: $3
=================================
]]></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>
验证方式, 在python文件夹下, 输入hello按下tab, 会出现已经定义的代码片段, 不停的按下tab会发现输入光标在$1, $2, $3的位置跳转, 跳转顺序由数字由小到大决定, Shift+Tab可以进行向上跳转, 可以通过Esc结束跳转
4. snippet Mirrored Fields
设置snippet镜像区域,会使相同编号的位置同时进行编辑
<snippet>
<content><![CDATA[
=================================
First Name: $1
Second Name: $1
Address: $1
=================================
]]></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>
验证方法: 在python文件中, 输入hello按下tab,出现代码片段,会出现三行同行编辑的光标, 这时进行编辑可以同时进行三行相同的编辑
5. snippet Placeholders
snippet 占位符含义类似于python的默认参数, 通过对Field做出一点修改, 可以定义Field的默认值, 并且可以通过tab键可以对不同的默认值进行修改
<snippet>
<content><![CDATA[
=================================
First Name: ${1:Guillermo}
Second Name: ${2:López}
Address: ${3:Main Street 1234}
User name: $1
Environment Variable : ${4:$TM_FILEPATH } #可以设置默认占位符为环境变量
Test: ${5:Nested ${6:Placeholder}}
=================================
]]></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>
验证方式: 在pyton文件中输入hello,然后按下tab, 输入代码片段后, 两个$1的field可以同时修改默认值, 然后继续按下tab键可以修改$2的默认值..., 还可以占位符设置嵌套
参考:
创建snippet