Snippets in Visual Studio Code
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. Join VS Code at Microsoft Ignite or catch the sessions online. A year of VS Code, wrapped. Explore the highlights. Snippets in Visual Studio Code View as Markdown Ask in VS Code Ask in VS Code Insiders Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements.
In Visual Studio Code, snippets appear in IntelliSense ( ⌃Space (Windows, Linux Ctrl+Space ) ) mixed with other suggestions, as well as in a dedicated snippet picker ( Insert Snippet in the Command Palette). There is also support for tab-completion: Enable it with "editor.tabCompletion": "on" , type a snippet prefix (trigger text), and press Tab to insert a snippet. The snippet syntax follows the TextMate snippet syntax with the exceptions of 'interpolated shell code' and the use of \u ; both are not supported.
Built-in snippets VS Code has built-in snippets for a number of languages such as: JavaScript, TypeScript, Markdown, and PHP. You can see the available snippets for a language by running the Insert Snippet command in the Command Palette to get a list of the snippets for the language of the current file. However, keep in mind that this list also includes user snippets that you have defined, and any snippets provided by extensions you have installed.
Install snippets from the Marketplace Many extensions on the Visual Studio Marketplace include snippets. You can search for extensions that contains snippets in the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X ) ) using the @category:"snippets" filter. If you find an extension you want to use, install it, then restart VS Code and the new snippets will be available. Create your own snippets You can easily define your own snippets without any extension.
To create or edit your own snippets, select Configure Snippets under File > Preferences , and then select the language (by language identifier ) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages. VS Code manages the creation and refreshing of the underlying snippets file(s) for you. Snippets files are written in JSON, support C-style comments, and can define an unlimited number of snippets.
Snippets support most TextMate syntax for dynamic behavior, intelligently format whitespace based on the insertion context, and allow easy multiline editing. Below is an example of a for loop snippet for JavaScript: // in file 'Code/User/snippets/javascript.json' { "For Loop" : { "prefix" : [ "for" , "for-const" ], "body" : [ "for (const ${2:element} of ${1:array}) {" , " \t $0" , "}" ], "description" : "A for loop." } } In the example above: "For Loop" is the snippet name.
It is displayed via IntelliSense if no description is provided. prefix defines one or more trigger words that display the snippet in IntelliSense. Substring matching is performed on prefixes, so in this case, "fc" could match "for-const". body is one or more lines of content, which will be joined as multiple lines upon insertion. Newlines and embedded tabs will be formatted according to the context in which the snippet is inserted. description is an optional description of the snippet displayed by IntelliSense.
Additionally, the body of the example above has three placeholders (listed in order of traversal): ${1:array} , ${2:element} , and $0 . You can quickly jump to the next placeholder with Tab , at which point you may edit the placeholder or jump to the next one. The string after the colon : (if any) is the default text, for example element in ${2:element} . Placeholder traversal order is ascending by number, starting from one; zero is an optional special case that always comes last, and exits snippet mode with the cursor at the specified position.
File template snippets You can add the isFileTemplate attribute to your snippet's definition if the snippet is intended to populate or replace a file's contents. File template snippets are displayed in a dropdown when you run the Snippets: Fill File with Snippet command in a new or existing file. Snippet scope Snippets are scoped so that only relevant snippets are suggested. Snippets can be scoped by either: the language(s) to which snippets are scoped (possibly all) the project(s) to which snippets are scoped (probably all) Language snippet scope Every snippet is scoped to one, several, or all ("global") languages based on whether it is defined in: a language snippet file a global snippet file Single-language user-defined snippets are defined in a specific language's snippet file (for example javascript.json ), which you can access by language identifier through Snippets: Configure Snippets .
A snippet is only accessible when editing the language for which it is defined. Multi-language and global user-defined snippets are all defined in "global" snippet files (JSON with the file suffix .code-snippets ), which is also accessible through Snippets: Configure Snippets . In a global snippets file, a snippet definition may have an additional scope property that takes one or more language identifiers , which makes the snippet available only for those specified languages.
If no scope property is given, then the global snippet is available in all languages. Most user-defined snippets are scoped to a single language, and so are defined in a language-specific snippet file. Project snippet scope You can also have a global snippets file (JSON with file suffix .code-snippets ) scoped to your project. Project-folder snippets are created with the New Snippets file for '<folder-name>'... option in the Snippets: Configure Snippets dropdown menu and are located at the root of the project in a .vscode folder.
Project snippet files are useful for sharing snippets with all users working in that project. Project-folder snippets are similar to global snippets and can be scoped to specific languages through the scope property. File pattern scope You can further control when snippets appear by using the optional include and exclude properties to specify file patterns. These properties work with both language-specific and global snippet files, and can be combined with the scope property for more precise control over snippet suggestions. include - A glob pattern or array of glob patterns that specifies which files the snippet should appear in. exclude - A glob pattern or array of glob patterns that specifies which files the snippet should not appear in.
Pattern matching works as follows: Filename-only patterns (for example, *.test.ts ) match based on the filename, regardless of the file's location in your project. Path-based patterns (for example, **/*.test.ts or **/dist/** ) match against the full file path. If a file matches both include and exclude patterns, the exclude pattern takes precedence. If neither property is specified, the snippet appears in all applicable files based on the scope property.
Examples Example: Test snippets This snippet only appears in TypeScript test files: { "Test Block" : { "prefix" : "test" , "body" : [ "test('${1:description}', () => {" , " \t ${0}" , "});" ], "description" : "Insert a test block" , "scope" : "typescript" , "include" : [ "**/*.test.ts" , "**/*.spec.ts" ] } } Example: Excluding directories This snippet appears in all JavaScript files except those in dist or node_modules directories: { "Console Log" : { "prefix" : "log" , "body" : "console.log(${0});" , "description" : "Insert console.log" , "scope" : "javascript" , "exclude" : [ "**/dist/**" , "**/node_modules/**" ] } } Example: Configuration file snippet This snippet only appears in travis.yml files, using a filename-only pattern: { "Travis CI Node" : { "prefix" : "travis-node" , "body" : [ "language: node_js" , "node_js:" , " - ${1:18}" ], "description" : "Travis CI Node.js configuration" , "scope" : "yaml" , "include" :
The rest of this page is on the live site.