sublime自定义代码段
2016/03/12    标签: 51    sublime   

像很多编程软件sublime也支持自定义代码段snippet。


菜单-Tools--New Snippet
<pre>代码段包含了所有当sublime text选择和插入时需要知道的信息。让我们按顺序来看看每一部分的功能。

content
实际插入文本的代码段。代码段模版很高级,呆会让我们看个例子。
写代码段时注意事项:
1. 如果你想要显示一个$符号,你需要这么写:$;
2. 当默认配置项translate_tabs_to_spaces(菜单--Preferences--setting-default)设置为True时,如果代码段中包含制表符,那么制表符将被转换成空格;
3.代码段必须包含在<![CDATA[…]]>内,否则无法工作;

tabTrigger
定义你插入代码时使用的自动补全的键,输入后你按tab键将补全代码段。

scope
定义代码段响应的范围,比如,html内,js,css,php等等

description
显示在代码提示片段中,如果不存在默认显示片段文件的名称。

代码片段特色
环境变量
代码片段可以访问环境变量,代码段自动使用sublime text 下的环境变量的值;
我们也可以在.sublime-options文件中定义自己的变量

sublime默认已定义变量</pre>

$PARAM1, $PARAM2…

Arguments passed to the insert_snippet command. (Not covered here.)

$SELECTION

The text that was selected when the snippet was triggered.

$TM_CURRENT_LINE

Content of the line the cursor was in when the snippet was triggered.

$TM_CURRENT_WORD

Current word under the cursor when the snippet was triggered.

$TM_FILENAME

File name of the file being edited including extension.

$TM_FILEPATH

File path to the file being edited.

$TM_FULLNAME

User’s user name.

$TM_LINE_INDEX

Column the snippet is being inserted at, 0 based.

$TM_LINE_NUMBER

Row the snippet is being inserted at, 1 based.

$TM_SELECTED_TEXT

An alias for $SELECTION.

$TM_SOFT_TABS

YES if translate_tabs_to_spaces is true, otherwise NO.

$TM_TAB_SIZE

Spaces per-tab (controlled by the tab_size option).

<pre>
例如
1.
====================================USER NAME: $TM_FULLNAMEFILE NAME: $TM_FILENAME TAB SIZE: $TM_TAB_SIZESOFT TABS: $TM_SOFT_TABS====================================
# Output:====================================USER NAME: guillermoFILE NAME: test.txt TAB SIZE: 4SOFT TABS: YES====================================</pre>
2.占位符嵌套


3.占位符正则替换

Original: ${1:Hey, Joe!}
Transformation: ${1/./=/g}

# Output:
Original: Hey, Joe!
Transformation: =========


Sublime Text错误提示Empty key解决方案