How to Create and Use Custom Code Snippets in VS Code

Create your own code snippets in VS Code. Learn the JSON format, tab stops, placeholders, choices, variables, and how to scope snippets to specific languages or projects.

6 min read

⚠ This article has a formatting issue and may not display correctly.

Our team has been notified. The content is shown as plain text below.

Code snippets are templates that expand a short trigger word into a block of code. VS Code comes with built-in snippets for JavaScript, TypeScript, Markdown, and PHP. You can add your own for any language. A custom snippet saves you from typing the same boilerplate: a React component skeleton, a Python test function, a SQL query template, or a license header. For related editing features, see [how to use IntelliSense](/vscode/how-to-use-intellisense-and-code-completion-in-vs-code) and the [keyboard shortcuts cheat sheet](/vscode/vs-code-keyboard-shortcuts-cheat-sheet-for-windows-macos-and-linux). ## Create your first snippet Open the Command Palette (**Ctrl+Shift+P** / **Cmd+Shift+P**) and run **Snippets: Configure Snippets**. You see a list of language choices and the **New Global Snippets file** option. Pick a language, for example javascript.json for JavaScript snippets. VS Code opens the JSON file where your snippets live. Replace the file contents with this: { "Log to console": { "prefix": "log", "body": "console.log('$1:', $1);$0", "description": "Insert console.log with a labeled variable" } } Save the file. Open a JavaScript file, type log, and press **Tab** (or **Enter** from the suggestion list). It expands to: console.log('|:', |); The cursor sits at the first $1 position, and both occurrences are linked: typing one updates the other. Press **Tab** to jump to $0, the final position. ## How snippet fields work Each snippet has four fields: | Field | Required | Purpose | |---|---|---| | "Snippet Name" | Yes | The display name shown in the suggestion list and the Insert Snippet picker | | prefix | Yes | The trigger text. Can be a single string or an array of strings | | body | Yes | The code inserted. An array of strings, one per line | | description | No | Extra text shown in the IntelliSense suggestion | The body uses special syntax for interactive behavior: | Syntax | What it does | |---|---| | $1, $2, $3 | Tab stops in order. Press Tab to move to the next one | | $0 | Final cursor position. Always visited last | | `${1:default}` | Placeholder with default text. The text is selected on insertion | | `${1|one,two,three|}` | Dropdown choice. User picks one value | | $TM_FILENAME | Built-in variable. Inserts the current file name | ## Tab stops and placeholders This snippet creates a React function component: { "React Function Component": { "prefix": "rfc", "body": [ "export function ${1:${TM_FILENAME_BASE}}() {", " $2", " return $0;", "}" ] } } The tab stops guide you through the component in order: The component name is selected and defaulted to the filename. Type a name and both linked occurrences update at once. Press **Tab** to jump to $2, where you add any logic the component needs. Press **Tab** to reach $0, the final cursor position inside the JSX. ## Dropdown choices Use `${1|optionA,optionB,optionC|}` to let the user pick from a predefined list: { "HTTP Status": { "prefix": "status", "body": "res.status(${1|200,201,204,400,401,403,404,500|}).json({ $0 });", "description": "Express response with status code choice" } } When inserted, a dropdown appears with the status codes. Pick one and move on. ## Snippet variables VS Code provides built-in variables you can use directly in snippet bodies: | Variable | Inserts | |---|---| | $TM_SELECTED_TEXT | The currently selected text | | $TM_CURRENT_LINE | The current line | | $TM_FILENAME | The full filename | | $TM_FILENAME_BASE | Filename without extension | | $TM_DIRECTORY | The file's directory | | $TM_FILEPATH | The full file path | | $CLIPBOARD | Your clipboard contents | | $CURRENT_YEAR | Current year (2026) | | $CURRENT_MONTH | Current month (01-12) | | $CURRENT_DATE | Current day (01-31) | | $UUID | A version 4 UUID | | $LINE_COMMENT | Line comment token for the current language | | $BLOCK_COMMENT_START | Block comment start for the current language | Example: a file header comment that adapts to any language: { "File Header": { "prefix": "header", "body": [ "$BLOCK_COMMENT_START", " * $TM_FILENAME - $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE", " * $0", " $BLOCK_COMMENT_END" ] } } This inserts a block comment in JavaScript, an HTML comment in HTML, and the correct comment syntax for any language the snippet appears in. ## Scope snippets to languages and projects ### Language snippets Snippets created in javascript.json only appear when editing JavaScript files. They are the simplest to manage. ### Global snippets with scope Global snippets (files ending in .code-snippets) appear in all languages unless you add a scope property: { "Import React": { "scope": "javascript,typescript", "prefix": "imr", "body": "import React from 'react';", "description": "Import React default export" } } ### Project snippets From the **Snippets: Configure Snippets** dropdown, select **New Snippets file for ''**. The file is created in .vscode/ at your project root. Commit it to Git and your whole team gets the snippets. Project snippets are global by default. Add a scope property to limit them to specific languages. ## Assign a keyboard shortcut to a snippet Bind a keyboard shortcut to a snippet for instant insertion. Open keybindings.json and add: { "key": "ctrl+shift+l", "command": "editor.action.insertSnippet", "when": "editorTextFocus", "args": { "snippet": "console.log('$1:', $1);$0" } } This inserts the snippet without opening any picker. You can also reference an existing snippet by langId and name instead of writing the body inline. ## Find and use snippets Open the Command Palette and run **Insert Snippet**. You see every snippet available for the current language, including built-in snippets, your custom snippets, and snippets from installed extensions. To remove a snippet from the suggestion list without deleting it, click the **Hide from IntelliSense** icon next to the snippet in the Insert Snippet picker. ## Customize snippet display Two settings control how snippets appear alongside regular suggestions. Setting `editor.snippetSuggestions` to top shows snippets above other IntelliSense results. Setting `editor.tabCompletion` to onlySnippets lets Tab insert the best matching snippet directly, without opening the suggestion list first. { "editor.snippetSuggestions": "top", "editor.tabCompletion": "onlySnippets" } With tabCompletion set this way, type the snippet prefix and press Tab. The snippet expands immediately. If no snippet matches, Tab inserts normal indentation.
Rune AI

Rune AI

Key Insights

  • Create snippets from the Command Palette: Snippets: Configure Snippets > choose a language or New Global Snippets file.
  • Each snippet has a name, a prefix (the trigger text), a body (the inserted code), and an optional description.
  • Use $1, $2 for tab stops in insertion order. Use $0 for the final cursor position. Use ${1:placeholder} for default values that get selected on insertion.
  • Use ${1|one,two,three|} to create a dropdown choice: the user picks from a list when the snippet is inserted.
  • Project snippets saved in .vscode/ can be committed to Git and shared with your team. Use the scope property to limit global snippets to specific languages.
RunePowered by Rune AI

Frequently Asked Questions

Where are my snippet files stored?

Language-specific snippets are stored in your user data folder: %APPDATA%\Code\User\snippets\ on Windows, $HOME/Library/Application Support/Code/User/snippets/ on macOS, and $HOME/.config/Code/User/snippets/ on Linux. Global snippets end in .code-snippets and are stored in the same folder. Project snippets live in the .vscode folder at the root of your project.

Can I share snippets with my team?

Yes. Create a global snippets file with the New Snippets file for '<project>' option. It saves into the .vscode folder. Commit this file to Git and everyone on the team gets the same snippets. Use the scope property to limit snippets to specific languages.

Can I bind a keyboard shortcut directly to a snippet?

Yes. Open keybindings.json and add an entry with "command": "editor.action.insertSnippet", "args": { "snippet": "your snippet body here" }, and your preferred key. You can also reference an existing snippet by name and language ID instead of inlining the body.

Conclusion

Custom snippets turn repetitive typing into a few keystrokes. Create them from the Command Palette with Snippets: Configure Snippets. Use $1, $2 for tab stops, ${1:default} for placeholders, and ${1|one,two|} for dropdown choices. Scope snippets to languages, projects, or specific file patterns. Bind your most-used snippets to keyboard shortcuts for instant insertion. For IntelliSense customization around snippets, see how to use IntelliSense.