Code snippets are one of the fastest ways to write repetitive code in Sublime Text. They let you insert predefined templates with a simple keyword and the Tab key, improving both speed and consistency.
How Do You Create a Snippet?
Creating a snippet is simple:
- Open Tools → Developer → New Snippet.
- Replace the default content with your own template.
- Set a
tabTriggerto define the keyword. - Save the file with the
.sublime-snippetextension inside the Packages/User folder. - Type the trigger and press Tab to insert the snippet.
5.1. Or Tools/Snippets/Select the snippet by name
Why Use Code Snippets?
Using snippets helps you:
- Write repetitive code faster.
- Reduce typing mistakes.
- Keep code formatting consistent.
- Reuse common templates.
- Improve productivity.
They're especially useful for HTML, CSS, JavaScript, PHP, Python, SQL, regular expressions, and boilerplate code.
Sublime Snippet Generator
Instead of writing XML manually, you can use a Sublime snippet generator to create valid .sublime-snippet files automatically. Simply enter your snippet content, choose a tab trigger, and save the generated file.
Example: Keep Only Hash Lines
The following snippet inserts a regular expression that matches only lines beginning with #:
<snippet>
<content><![CDATA[
^(?=\s*\$|(?!(\s*\#))).*\R?
]]></content>
<tabTrigger>keeponlyhash</tabTrigger>
</snippet>
Save it as keeponlyhash.sublime-snippet, type keeponlyhash, and press Tab to insert the expression instantly.
Potential Issues and Caveats
Although Sublime Text snippets are easy to use, there are a few common pitfalls to be aware of.
Escape Special Characters
Snippets are stored as XML, so characters such as <, >, and & should be wrapped inside a <![CDATA[ ... ]]> block. Otherwise, the snippet may not load correctly.
Line Endings
Different operating systems use different line endings (LF on Linux/macOS and CRLF on Windows). If your snippet contains regular expressions or multiline text, unexpected line endings can change its behavior. Test snippets after moving them between operating systems or version control systems.
Preserve Indentation
Sublime Text automatically adjusts indentation in many cases. If your snippet relies on exact spacing or tabs, verify that the inserted text matches your expectations.
Tab Trigger Conflicts
Avoid using generic tab triggers such as if, for, or div. If multiple snippets share the same trigger, Sublime Text may present multiple completion options or insert an unexpected snippet.
Scope Matters
A snippet can be limited to a specific language using the <scope> element. If the scope does not match the current file type, the snippet will not appear in autocomplete.
Example:
<scope>source.python</scope>
Variables and Placeholders
Snippet variables ($TM_FILENAME, $SELECTION, etc.) and placeholders ($1, $2) are expanded automatically. If you need to insert a literal dollar sign, escape it as \$.
File Location
Snippets must be saved with the .sublime-snippet extension, typically in the Packages/User directory. Files saved elsewhere may not be recognized by Sublime Text.
Test After Editing
Sublime Text usually reloads snippets automatically after saving. If changes do not appear, ensure the snippet XML is valid and restart Sublime Text if necessary.
Final Thoughts
Sublime Text snippets are a simple but powerful productivity feature. Whether you create snippets manually or use a snippet generator, they can save time, reduce repetitive typing, and make your workflow much more efficient.