Create Sublime Text Snippets
Whether you are coding or writing the next vampire best-seller, you’re likely to need certain short fragments of text again and again. Use snippets to save yourself tedious typing. Snippets are smart templates that will insert text for you and adapt it to their context.
Hit Tools -> New Snippet...
from the top menubar. A new file will open, with this in it:
<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>
Snippets use markers ${1}
to cycle through positions within the snippet by pressing the Tab key. {$1}
is the first marker, {2}
is the second, etc..
The above example also uses placeholders
(${1:this}
), so if you hit tab
it’ll move to the next placeholder and select it.
You need to un-comment <tabTrigger>
and <scope>
to control which key executes the snippet, and on what files will it work on.
Example of a snippet for Javascript files, that will replace log
with console.log();
:
<snippet>
<content><![CDATA[
console.log(${1});
${2}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>log</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.js</scope>
</snippet>
Finding the right scope
To find out what scope
to assign the snippet to, use ctrl+alt+shift+p
, it’ll show the current scope on the status bar.
For example: text.html.markdown
for .md
(markdown) files.