现在介绍一下Snippet, Snippets are smart templates that will insert text for you and adapt it to their context. Snippet 是插入到文本中的智能模板并使这段文本适当当前代码环境. 程序员总是会不断的重写一些简单的代码片段, 这种工作乏味/无聊, 而Snippet的出现会让Code更加高效.
Snippet文件是以.sublime-snippet为扩展的XML文件, 可以命名为XXX.sublime-snippet, 创建自己的snippet的方式为菜单栏Tools | New Snippet..
下面看一下新建的文件格式:
1 2 3 4 5 6 7 8 9
<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>
为了方便理解简化以上代码:
1 2 3 4 5 6 7 8 9
<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>
简要介绍一下snippet四个组成部分:
content:其中必须包含<![CDATA[…]]>,否则无法工作, Type your snippet here用来写你自己的代码片段
tabTrigger:用来引发代码片段的字符或者字符串, 比如在以上例子上, 在编辑窗口输入hello然后按下tab就会在编辑器输出Type your snippet here这段代码片段
<snippet> <content><![CDATA[ ================================= $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包含几个空格 ================================= ]]></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>
验证方式 : 保存自定义snippet,在python文件夹下输入hello按下tab
#3. snippet Fields
设置Fields, 可以通过tab键循环的改变代码片段的一些值
1 2 3 4 5 6 7 8 9 10 11 12 13
<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>
<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>
<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>
""" ]]></content> <!-- Optional: Set a tabTrigger to define how to trigger the snippet --> <tabTrigger>"""</tabTrigger> <!-- Optional: Set a scope to limit where the snippet will trigger --> <scope>source.python</scope> <!-- Optional: Description to show in the menu --> <description>Documentation Comments</description> </snippet>