Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 40 ยท Editing and Navigation

Snippets and Emmet

Use built-in snippets and Emmet abbreviations in VS Code, then create your own snippet files with tab stops, placeholders, choices and variables, plus a runnable expander model.

Much code is boilerplate: a for loop, an HTML skeleton, a test function, a React component. Snippets are templates that expand from a short prefix into a block of code with places for you to fill in. Emmet is a related tool for HTML and CSS that expands tiny abbreviations into large markup. Both are built into VS Code.

Using built-in snippets

Start typing a prefix and IntelliSense shows a suggestion with a small snippet icon. Press Tab (or Enter) to expand it. Then press Tab to move to the next placeholder and Shift+Tab to go back. After the last stop, Tab leaves the snippet.

In JavaScript, for example, typing for offers for, foreach, forof and forin snippets. In HTML, ! plus Tab inserts a full HTML5 skeleton. The command Insert Snippet in the Command Palette shows all snippets for the current language, with their descriptions.

If suggestions do not appear while typing, check editor.quickSuggestions and editor.tabCompletion (set to on to expand a snippet with Tab even when the list is closed).

Emmet

Emmet turns a CSS-selector-like abbreviation into HTML. It works in HTML, CSS, JSX and many template languages. Type the abbreviation and press Tab (or Enter).

Abbreviation Expands to
! HTML5 document skeleton
p <p></p>
div.card <div class="card"></div>
#app <div id="app"></div>
ul>li*3 A list with three items
ul>li.item$*3 Three items with classes item1, item2, item3
h1+p Siblings: heading followed by a paragraph
nav>ul>li>a Nested elements (child with >)
a[href=#]{Click} <a href="#">Click</a>
input:email An email input
lorem10 Ten words of placeholder text

Operators:

  • > child, + sibling, ^ climb up one level
  • *n repeat n times, $ counter (use $$ for zero padding)
  • .name class, #id id, [attr=value] attributes
  • text content, ( ... ) grouping

The same engine works in CSS: m10 gives margin: 10px;, df gives display: flex;, w100p gives width: 100%;, pos:r gives position: relative;.

Wrap with abbreviation (Emmet: Wrap with Abbreviation) wraps selected text: select lines, run the command and type ul>li* to turn each line into a list item.

A stand-in: a tiny Emmet

The real Emmet is much richer. This JavaScript stand-in supports only tag names, classes, ids, > and *n, so you can see the idea: parse the abbreviation, then print markup. It does not call VS Code.

JS Stand-in: a mini Emmet expander

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Writing your own snippets

Open the Command Palette and run Snippets: Configure Snippets. Pick a language (creating python.json, for instance) or New Global Snippets file or New Snippets file for 'project name' (stored in .vscode/*.code-snippets, so the team shares it). A snippet file is JSONC:

JSON
{
  "Console log": {
    "prefix": "clg",
    "body": ["console.log('$1:', $1);", "$0"],
    "description": "Log a labelled value"
  },
  "Test case": {
    "prefix": "tc",
    "body": [
      "test('${1:does something}', () => {",
      "\t$0",
      "});"
    ]
  }
}
  • prefix: what you type to trigger it (a string or an array of strings).
  • body: the text, as one string or an array of lines. Use \t for tab and \\ for a backslash.
  • description: shown in the suggestion list.
  • scope (global files only): language IDs, e.g. "javascript,typescript".

Tab stops, placeholders and choices

Syntax Meaning
$1, $2 Tab stops, visited in numeric order
$0 Final cursor position
${1:default} Placeholder with default text
${1|one,two,three|} A choice menu
$1 used twice Mirrored: typing in one edits both
${1/regex/format/flags} Transform the value

Variables

Snippets can insert context:

  • $TM_FILENAME, $TM_FILENAME_BASE, $TM_DIRECTORY
  • $TM_SELECTED_TEXT, $TM_CURRENT_LINE
  • $CURRENT_YEAR, $CURRENT_MONTH, $CURRENT_DATE
  • $CLIPBOARD (the copied text)
  • $RANDOM, $UUID
  • $BLOCK_COMMENT_START, $LINE_COMMENT for the language's comment syntax

Example: a class skeleton named after the file:

JSON
"Class from file": {
  "prefix": "cls",
  "body": ["export class ${1:$TM_FILENAME_BASE} {", "\tconstructor() {$0}", "}"]
}

A stand-in: expanding a snippet body

This stand-in expands $1-style tab stops, ${1:default} placeholders and a few variables into plain text, and lists the tab stop order. It shows what the editor does when it parses the body field. It does not model the interactive Tab key.

JS Stand-in: expand a snippet

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output shows the default text filled in, the variable values substituted and the order in which Tab visits stops. In the editor the placeholders are selected so typing replaces them.

Snippets from extensions

Many language extensions ship snippets (React, Vue, Django, test frameworks), and you can install snippet-only extensions. To see what you have, run Snippets: Fill Snippet / Insert Snippet. A prefix collision is resolved by the suggestion list; type more letters to narrow it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Emmet abbreviation ul>li*3 produce?

    means child and *3 repeats the li three times inside the ul.

  2. 2. What does $0 mean in a snippet body?

    $0 is where the cursor ends up after the last Tab press.
  3. 3. How can a team share project-specific snippets?

    A .code-snippets file in .vscode is loaded for that workspace and can be committed to Git.
  4. 4. What does ${1:name} define?

    The ${n:text} form is a placeholder: a tab stop with default text.

Summary

  • Type a prefix and press Tab to expand a snippet; Tab moves between placeholders.
  • Emmet expands abbreviations such as ul>li*3 in HTML, CSS and JSX.
  • Custom snippets live in language, global or project .code-snippets files.
  • Use $1, ${1:default}, choices, mirrors, $0 and variables like $TM_FILENAME_BASE.
  • Snippets save typing and enforce consistent boilerplate across a team.

Next: Formatting and Code Actions