A code snippet is a reusable template of source code that an editor inserts in response to a short trigger, so that frequently typed boilerplate can be produced with a few keystrokes. A programmer types a prefix such as “for” or “class”, accepts the suggestion, and the editor expands it into a full skeleton, leaving the cursor positioned at the parts that still need to be filled in. Snippets reduce repetitive typing and help enforce consistent structure for common constructs.
The mechanics of a snippet are described precisely in editor snippet documentation. As Visual Studio Code’s snippet documentation explains, a snippet has a body of one or more lines of content, and within that body special syntax marks the interactive parts. Tab stops are written as $1, $2, and so on, specifying the order in which the cursor visits each editable location, while $0 denotes the final cursor position after the snippet is fully filled in. Pressing the tab key moves the cursor from one tab stop to the next.
Tab stops can carry default text, in which case they are called placeholders, written as ${1:foo}. When the snippet expands, the placeholder text is inserted and selected so it can be easily replaced or kept. Snippets can also include variables such as ${name:default}, which insert contextual values like the current file name, the selected text, or the date, falling back to a default or empty string when the variable is not set. More advanced syntax allows choices, where a tab stop offers a menu of options, and transforms that reshape inserted text with regular expressions.
Snippet formats grew out of the broader tradition of editor templates, and the syntax popularized by tools like TextMate, with its numbered tab stops and placeholders, became a de facto standard adopted by many later editors. Because the format is largely shared, a snippet written for one editor often transfers with little change to another, and language extensions can ship snippet collections that work across compatible tools.
Snippets sit alongside code completion and other editing aids as part of what makes a modern editor productive. Where completion suggests an existing symbol, a snippet generates a whole multi-line structure with the editable spots already marked, turning a memorized pattern into a single trigger word.