keepcalculator

06 Numbers and Powers

Sublime Text Snippets: Create and Use Them

Card No. 06·60335 · Updated · Standard formula, shown below

Sublime Text snippets are reusable pieces of text or code that you insert by typing a trigger and accepting a completion. They are useful for anything you repeat, from a function template to a standard email reply, and can include tab stops, placeholders, and dynamic values.

What a snippet is

A snippet is a small template stored in a file with the .sublime-snippet extension. When you invoke it, Sublime Text inserts its contents into the current document. A simple snippet might expand sig into a signature, while a programming snippet could create a function skeleton with the cursor positioned at the first value you need to fill in.

Snippets are distinct from build systems, macros, and plugins. They do not normally run code or automate a sequence of application commands; their main job is to insert text. They can, however, use Sublime Text’s snippet syntax for placeholders and limited contextual behavior.

Create and save a snippet

Use the built-in snippet command to create a file:

  1. Open the Command Palette using Ctrl+Shift+P on Windows or Linux, or Cmd+Shift+P on macOS.
  2. Search for Snippet: New Snippet and select the command.
  3. Replace the sample content in the new file, then save it with a descriptive name ending in .sublime-snippet.

A basic snippet file looks like this:

<snippet>
    <content><![CDATA[
Hello, ${1:name}!
$0
]]></content>
    <tabTrigger>hello</tabTrigger>
    <description>Insert a greeting</description>
</snippet>

The outer XML elements define the snippet. The CDATA section lets the body contain characters such as angle brackets without treating them as XML markup. Save the file in Sublime Text’s suggested user snippets location when you want it available as your own snippet; the command and file dialog help select the relevant location.

Triggers, scope, and insertion

The tabTrigger is the short text used to find a snippet. With the example above, type hello and press Tab when the snippet is offered as a completion. You can also use the completion menu or the Command Palette’s snippet insertion command. The exact interaction can depend on your key bindings and completion settings.

To limit a snippet to particular file types, add a scope element, for example:

<scope>source.python</scope>

Scopes are syntax identifiers used by Sublime Text, not ordinary file extensions. A scope restriction keeps a Python-specific template from appearing in unrelated documents. To check the scope at the cursor, use the Command Palette and search for the scope-inspection command; the status bar can also show syntax information depending on your settings.

If two snippets share a trigger, the completion list may offer multiple choices. Distinctive triggers, useful descriptions, and appropriate scopes make the intended item easier to select. A snippet can also be inserted from the Command Palette, which is helpful when you do not remember its trigger.

Tab stops, placeholders, and variables

Snippet fields let you move through a template with the Tab key. Numbered tab stops mark editable positions: ${1:name} creates the first stop with name as its default text, and ${2:value} creates the second. Pressing Tab advances between stops in numerical order. $0 marks the final cursor position after the other fields.

For example, this template provides a short function outline:

def ${1:function_name}(${2:arguments}):
    ${3:pass}
$0

Defaults are useful prompts, not mandatory values: selecting a stop lets you replace its text. You can use the same numbered field more than once to create linked fields, so editing one occurrence updates the others. This is handy for a name repeated in a declaration and a call.

Sublime Text also supports snippet variables, such as $TM_FILENAME for the current file name and $CURRENT_YEAR for the current year. Available variables are expanded when the snippet is inserted. If literal text looks like snippet syntax, escape dollar signs or other special characters as needed; otherwise Sublime Text may interpret them as fields or variables.

Choosing between snippets and other tools

Snippets suit short, repeatable text with a few editable locations. They are a good fit for boilerplate that belongs in the document, such as a comment header, a common markup element, or a test outline. They are less suitable for complex workflows that need to inspect files, call external tools, or make decisions; those tasks may require a plugin, build system, or script instead.

  • Use a snippet when you want quick insertion of a stable template with a small number of fields.
  • Use a macro when you need to record or replay editor actions rather than insert a reusable text template.
  • Use a plugin or script when the task needs logic, external data, or coordinated changes beyond snippet substitution.

Snippets can be shared as files, but they are not automatically portable between every editor. Keep a copy in a version-controlled settings repository or synchronize your user settings if you want the same templates on multiple machines. Check that the recipient’s editor supports Sublime Text’s snippet format before sharing it as a ready-to-use file.

Common problems and fixes

If a snippet does not appear, first confirm that the file was saved with the .sublime-snippet extension and in a location Sublime Text loads. Check the trigger spelling and make sure the current document matches the snippet’s scope. A snippet restricted to Python, for example, will not be offered in a plain-text file.

If pressing Tab inserts an indent instead, look at the completion menu and key bindings. Tab may be configured for indentation or another action, and a completion may need to be selected first. Try invoking the snippet through the Command Palette to separate a discovery problem from a key-binding problem.

Malformed XML can prevent a snippet from loading. Ensure the opening and closing tags match, keep the body inside the content element, and preserve the CDATA markers around code that contains XML-sensitive characters. If an edit does not seem to take effect, save the file and try invoking the snippet again; restarting the editor can help determine whether a loading or syntax issue remains.

Frequently Asked Questions

Where are Sublime Text snippets saved?
Save personal snippets in Sublime Text’s user snippets location. The New Snippet command opens a template and helps direct you to a suitable save location; the exact settings path differs by operating system.

How do I trigger a snippet?
Type its tabTrigger and select it from the completion list, often by pressing Tab. You can also find snippets through the Command Palette.

Can I make a snippet work only in one language?
Yes. Add a scope element containing the relevant Sublime Text syntax scope, such as source.python. The snippet will then be offered only in matching contexts.

How do I move between snippet fields?
Use Tab to advance through numbered tab stops in order. The $0 marker indicates where the cursor should finish.

Can Sublime Text snippets insert the current file name?
Yes. Use a supported snippet variable such as $TM_FILENAME in the content. Variables are expanded when the snippet is inserted, so the result depends on the current editor context.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →