sublime text 2自訂程式碼片段

來源:互聯網
上載者:User

標籤:

本文引用   http://www.blogjava.net/Hafeyang/archive/2012/08/17/how_to_create_code_snippet_in_subline_text_2.html

 

 

對於前端工程師來講,寫一個html頁面的基本結構是體力活,每次去拷貝一個也麻煩,sublime text 2 提供了一個很好的複用程式碼片段。下面介紹一下建立一個html5的程式碼片段的過程。

在菜單上點擊Tools -> New Snippet,(工具->程式碼片段)會建立一個xml檔案頁簽:

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


注釋已經說的比較詳細了。

content 裡面就是代碼模版:${序號:預設值} ,序號相同的地方游標會同時停在那可以多處同時編輯。序號大小就是tabindex。在實際使用代碼的時候,可以使用tab切換游標位置。

tabTrigger是指輸入該字串後tab就是在游標插入content中的內容。

scope是指在何種檔案類型中使用。

下面是html5程式碼片段的定義:

<snippet>
    <content><![CDATA[
<!doctype html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>${1}</title> 
</head>
<body>
    <h1>${1}</h1>
    Hello, ${2:this} is a ${3:snippet}.
</body>
</html>
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>html5</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>


編輯完之後儲存為 C:\Users\[使用者]\AppData\Roaming\Sublime Text 2\Packages\User\html5.sublime-snippet  (Win7下) 預設的儲存路徑就行。尾碼必須是.sublime-snippet。

儲存完重啟Sublime text 2,建立檔案:輸入html5,tab會出現如下效果:



${1}出現了兩次,所以游標同時編輯圖中兩處。
${2:this},所以在2處出現this預設值。${1}處編輯完按tab就到${2}處。

OK, That‘s all。

sublime text 2自訂程式碼片段

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.