Original link: http://www.bluesdream.com/blog/sublime-text-snippets-function.html
When we write code, we always encounter some code snippets that need to be reused. This is the time to repeat the copy and paste, greatly affect the efficiency. We can solve this problem very well by using the snippet function of sublime text. Popular speaking, is to save our common code, and then through the form of plug-ins repeated calls.
How to create: Tools > New Snippet
At this point you will see the following sample code:
<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> |
At this point you should be a little inexplicable, we go on to see the complete structure and description:
< snippet > < content > <! [CDATA [code fragment you need to insert ${1: name}]] >. Appendix: source.css and test.html correspond to different files respectively. -->< scope > source. Python < / scope > <! -- optional: display instructions in snippet menu (Chinese supported). If not defined, the menu displays the file name of the current file. --> <description>My Fancy Snippet</description></snippet> |
${1:name} indicates where the cursor is positioned after the code is inserted, and can be inserted more than once. Where: Name is a custom parameter (optional).
${2} indicates that after the code is inserted, pressing the TAB key, the cursor jumps to the appropriate position (and so on) in order.
Now, you should have a general understanding. So we're going to start doing it ourselves. Write an instance:
< snippet > < content > <! [CDATA [< footer > < p > copyright © 2008-2012 ${1: bluesdream}. Com < / P > < p > value added telecommunication business license Shanghai B2 - ${2} < a href = "#" > Shanghai ICP backup number ${3} < / a > < p > < footer >]]] > <scope>text.html</scope></snippet> |
After creation, save in the \packages\user directory (example X:\Sublime Text 2.0\data\packages\user), the file is named Cft-code, suffix. Sublime-snippet.
At this point we open an HTML file, enter CFT, and press TAB, and the code snippet that we just wrote is plugged in. And at this point the cursor stays at the location of the ${1} we marked, and if we press TAB again, the cursor jumps to the position of ${2}. Since we have defined in scope only for use in HTML files, it is not possible to insert code snippets at this point if we open a file in CSS or other format.
Add: In addition to using the shortcut key to tab out the code, we can also load through the menu, open Tools > Snippet, select Snippet:custom-footer. If you do not define description, you will now see the Snippet:cft-code option named after our file name.
Sublime Text Snippets (code snippet) feature