手把手教你寫Sublime中的Snippet


Sublime Text號(hào)稱最性感的編輯器, 并且越來越多人使用, 美觀, 高效

關(guān)于如何使用Sublime text可以參考我的另一篇文章, 相信你會(huì)喜歡上的..
Sublime Text 2使用心得

現(xiàn)在介紹一下Snippet, Snippets are smart templates that will insert text for you and adapt it to their context. Snippet 是插入到文本中的智能模板并使這段文本適當(dāng)當(dāng)前代碼環(huán)境. 程序員總是會(huì)不斷的重寫一些簡(jiǎn)單的代碼片段, 這種工作乏味/無聊, 而Snippet的出現(xiàn)會(huì)讓Code更加高效.

1. Snippe創(chuàng)建,存儲(chǔ)和格式


(這里snippet稱作代碼片段)

Snippet可以存儲(chǔ)在任何的文件夾中, 并且以.sublime-snippet為文件擴(kuò)展名, 默認(rèn)是存儲(chǔ)在.sublime-snippet文件夾下.

Snippet文件是以.sublime-snippet為擴(kuò)展的XML文件, 可以命名為XXX.sublime-snippet, 創(chuàng)建自己的snippet的方式為菜單欄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>

為了方便理解簡(jiǎn)化以上代碼:

<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>

簡(jiǎn)要介紹一下snippet四個(gè)組成部分:

  • content:其中必須包含<![CDATA[…]]>,否則無法工作, Type your snippet here用來寫你自己的代碼片段
  • tabTrigger:用來引發(fā)代碼片段的字符或者字符串, 比如在以上例子上, 在編輯窗口輸入hello然后按下tab就會(huì)在編輯器輸出Type your snippet here這段代碼片段
  • scope: 表示你的代碼片段會(huì)在那種語言環(huán)境下激活, 比如上面代碼定義了source.python, 意思是這段代碼片段會(huì)在python語言環(huán)境下激活.
  • description :展示代碼片段的描述, 如果不寫的話, 默認(rèn)使用代碼片段的文件名作為描述

2. snippet環(huán)境變量


列舉一下可能用到的環(huán)境變量, 這些環(huán)境變量是在Sublime中已經(jīng)預(yù)定義的.

環(huán)境變量名 描述
$TM_FILENAME 用戶文件名
$TM_FILEPATH 用戶文件全路徑
$TM_FULLNAME 用戶的用戶名
$TM_LINE_INDEX 插入多少列, 默認(rèn)為0
$TM_LINE_NUMBER 一個(gè)snippet插入多少行
$TM_SOFT_TABS 如果設(shè)置translate_tabs_to_spaces : true 則為Yes
$TM_TAB_SIZE 每個(gè)Tab包含幾個(gè)空格

同一通過下面的代碼片段進(jìn)行驗(yàn)證:

<snippet>
   <content><![CDATA[
=================================
$TM_FILENAME   用戶文件名
$TM_FILEPATH   用戶文件全路徑
$TM_FULLNAME    用戶的用戶名
$TM_LINE_INDEX   插入多少列, 默認(rèn)為0
$TM_LINE_NUMBER   一個(gè)snippet插入多少行
$TM_SOFT_TABS  如果設(shè)置translate_tabs_to_spaces : true 則為Yes
$TM_TAB_SIZE   每個(gè)Tab包含幾個(gè)空格
=================================
]]></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>

驗(yàn)證方式 : 保存自定義snippet,在python文件夾下輸入hello按下tab

3. snippet Fields


設(shè)置Fields, 可以通過tab鍵循環(huán)的改變代碼片段的一些值

<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>

驗(yàn)證方式, 在python文件夾下, 輸入hello按下tab, 會(huì)出現(xiàn)已經(jīng)定義的代碼片段, 不停的按下tab會(huì)發(fā)現(xiàn)輸入光標(biāo)在$1, $2, $3的位置跳轉(zhuǎn), 跳轉(zhuǎn)順序由數(shù)字由小到大決定, Shift+Tab可以進(jìn)行向上跳轉(zhuǎn), 可以通過Esc結(jié)束跳轉(zhuǎn)

4. snippet Mirrored Fields


設(shè)置snippet鏡像區(qū)域,會(huì)使相同編號(hào)的位置同時(shí)進(jìn)行編輯

<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>

驗(yàn)證方法: 在python文件中, 輸入hello按下tab,出現(xiàn)代碼片段,會(huì)出現(xiàn)三行同行編輯的光標(biāo), 這時(shí)進(jìn)行編輯可以同時(shí)進(jìn)行三行相同的編輯

5. snippet Placeholders


snippet 占位符含義類似于python的默認(rèn)參數(shù), 通過對(duì)Field做出一點(diǎn)修改, 可以定義Field的默認(rèn)值, 并且可以通過tab鍵可以對(duì)不同的默認(rèn)值進(jìn)行修改

<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 }  #可以設(shè)置默認(rèn)占位符為環(huán)境變量
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>

驗(yàn)證方式: 在pyton文件中輸入hello,然后按下tab, 輸入代碼片段后, 兩個(gè)$1的field可以同時(shí)修改默認(rèn)值, 然后繼續(xù)按下tab鍵可以修改$2的默認(rèn)值..., 還可以占位符設(shè)置嵌套

寫到這里基本上大家都應(yīng)該可以根據(jù)需求編寫簡(jiǎn)單的snippet了, 恭喜你..

6. snippet Substitutions


高級(jí)應(yīng)用可以使用Perl的正則表達(dá)式

最后送上簡(jiǎn)單的python的snippet

<snippet>
    <content><![CDATA[
"""

文檔注釋

Args : 
    ${1}:

Returns : 
    ${2}:

Raises : 
    ${3}:

"""
]]></content>
    <tabTrigger>"""</tabTrigger>
    <scope>source.python</scope>
    <description>Documentation Comments</description>
</snippet>

###
<snippet>
    <content><![CDATA[def ${1:foo}():
    doc = "${2:The $1 property.}"
    def fget(self):
        ${3:return self._$1}
    def fset(self, value):
        ${4:self._$1 = value}
    def fdel(self):
        ${5:del self._$1}
    return locals()
$1 = property(**$1())$0]]></content>
    <tabTrigger>property</tabTrigger>
    <scope>source.python</scope>
    <description>New Property</description>
</snippet>

7. 拓展閱讀和參考鏈接


Snippets
Syntax Definitions
Perl Regular Expression Syntax
Boost-Extended Format String Syntax

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容