Snippets are templates that make it easier to enter repeating code patterns. VS Code includes built-in snippets for many languages and supports custom user-defined snippets.
Using Built-in Snippets
VS Code comes with many built-in snippets for popular languages:
Start Typing
Begin typing a snippet prefix in your editor. For example, type for in a JavaScript file.
View Suggestions
IntelliSense will show available snippets. Snippet suggestions are marked with a special icon.
Insert Snippet
Press Tab or Enter to insert the snippet. Use Tab to jump between placeholders.
Viewing Available Snippets
To see all available snippets for the current language:
Open Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
Type “Insert Snippet”
Browse the list of available snippets
Built-in Snippet Examples
Here are some built-in snippets from VS Code:
JavaScript Snippets
for loop (prefix: for)
foreach loop (prefix: foreach =>)
class (prefix: class)
async function (prefix: async function)
for ( let index = 0 ; index < array . length ; index ++ ) {
const element = array [ index ];
}
TypeScript Snippets
property getter (prefix: get)
property setter (prefix: set)
full property (prefix: prop)
public get value () : string {
return
}
HTML Snippets
html document (prefix: html or !)
<! DOCTYPE html >
< html >
< head >
< meta charset = "UTF-8" />
< title > title </ title >
</ head >
< body >
</ body >
</ html >
Creating Custom Snippets
You can create your own snippets for any language:
Open Snippets File
Open Command Palette and type “Configure User Snippets” or “Configure Snippets” Choose:
New Global Snippets file - Available in all projects
New Snippets file for ‘[Language]’ - Language-specific
[Language].json - Edit existing language snippets
Define Snippet
Add your snippet definition in JSON format
Use Snippet
Start typing the prefix to use your new snippet
Snippet Syntax
Snippets are defined in JSON with the following structure:
{
"Snippet Name" : {
"prefix" : "trigger" ,
"body" : [
"line 1" ,
"line 2"
],
"description" : "Description shown in IntelliSense"
}
}
Basic Example
{
"Console Log" : {
"prefix" : "log" ,
"body" : [
"console.log($1);" ,
"$0"
],
"description" : "Log to the console"
}
}
Snippet Features
Tabstops
Tabstops allow you to move the cursor through snippet fields:
{
"For Loop" : {
"prefix" : "for" ,
"body" : [
"for (let ${1:index} = 0; ${1:index} < ${2:array}.length; ${1:index}++) {" ,
" \t const ${3:element} = ${2:array}[${1:index}];" ,
" \t $0" ,
"}"
],
"description" : "For Loop"
}
}
$1, $2, $3 - Tab stops in order
$0 - Final cursor position
${1:index} - Placeholder with default text
Using the same number (like ${1:index}) in multiple places will update all occurrences simultaneously as you type.
Placeholders
Placeholders provide default values:
{
"React Component" : {
"prefix" : "rfc" ,
"body" : [
"function ${1:ComponentName}() {" ,
" \t return (" ,
" \t\t <div>" ,
" \t\t\t $0" ,
" \t\t </div>" ,
" \t );" ,
"}" ,
"" ,
"export default ${1:ComponentName};"
],
"description" : "React functional component"
}
}
Choice
Provide a dropdown of choices:
{
"Console Method" : {
"prefix" : "cons" ,
"body" : [
"console.${1|log,warn,error,info|}($2);" ,
"$0"
],
"description" : "Console with method choice"
}
}
Variables
Snippets can use predefined variables:
{
"File Header" : {
"prefix" : "header" ,
"body" : [
"/*" ,
" * File: $TM_FILENAME" ,
" * Project: $TM_DIRECTORY" ,
" * Created: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE" ,
" * Author: $1" ,
" */" ,
"$0"
],
"description" : "File header comment"
}
}
Common variables:
$TM_FILENAME - Current file name
$TM_FILENAME_BASE - File name without extension
$TM_DIRECTORY - Current directory
$TM_FILEPATH - Full file path
$CLIPBOARD - Clipboard contents
$CURRENT_YEAR - Current year
$CURRENT_MONTH - Month as two digits
$CURRENT_DATE - Day of month
$CURRENT_HOUR - Hour in 24-hour format
$CURRENT_MINUTE - Current minute
$CURRENT_SECOND - Current second
$BLOCK_COMMENT_START - Language-specific block comment start
$BLOCK_COMMENT_END - Language-specific block comment end
$LINE_COMMENT - Language-specific line comment
Transform variable values:
{
"Class from Filename" : {
"prefix" : "class" ,
"body" : [
"class ${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/} {" ,
" \t constructor($1) {" ,
" \t\t $0" ,
" \t }" ,
"}"
],
"description" : "Class named after file"
}
}
Multi-Language Snippets
Global snippets files (.code-snippets) can define snippets for multiple languages:
my-snippets.code-snippets
{
"Arrow Function" : {
"scope" : "javascript,typescript,javascriptreact,typescriptreact" ,
"prefix" : "af" ,
"body" : [
"const ${1:functionName} = (${2:params}) => {" ,
" \t $0" ,
"};"
],
"description" : "Arrow function"
}
}
Advanced Snippet Examples
Try-Catch with Error Handling
{
"Try-Catch Statement" : {
"prefix" : "trycatch" ,
"body" : [
"try {" ,
" \t $TM_SELECTED_TEXT$0" ,
"} catch (${1:error}) {" ,
" \t console.error(${1:error});" ,
"}"
],
"description" : "Try-Catch Statement"
}
}
Async Function with Error Handling
{
"Async Function with Try-Catch" : {
"prefix" : "afn" ,
"body" : [
"async function ${1:functionName}(${2:params}) {" ,
" \t try {" ,
" \t\t $0" ,
" \t } catch (error) {" ,
" \t\t console.error(error);" ,
" \t\t throw error;" ,
" \t }" ,
"}"
],
"description" : "Async function with try-catch"
}
}
React Hook
{
"useState Hook" : {
"prefix" : "us" ,
"body" : [
"const [${1:state}, set${1/(.*)/${1:/capitalize}/}] = useState$3(${2:initialValue});"
],
"description" : "React useState hook"
}
}
Test Case
{
"Test Case" : {
"prefix" : "test" ,
"body" : [
"test('${1:test description}', () => {" ,
" \t $0" ,
"});"
],
"description" : "Test case"
}
}
File Template Snippets
You can create snippets that are suggested when creating new files:
{
"React Component File" : {
"prefix" : "rfc" ,
"isFileTemplate" : true ,
"body" : [
"import React from 'react';" ,
"" ,
"interface ${TM_FILENAME_BASE}Props {" ,
" \t $1" ,
"}" ,
"" ,
"export const ${TM_FILENAME_BASE}: React.FC<${TM_FILENAME_BASE}Props> = (props) => {" ,
" \t return (" ,
" \t\t <div>" ,
" \t\t\t $0" ,
" \t\t </div>" ,
" \t );" ,
"};"
],
"description" : "React component with TypeScript"
}
}
Snippet Scope and Include/Exclude
Control where snippets appear:
{
"Test Snippet" : {
"prefix" : "test" ,
"scope" : "typescript,javascript" ,
"include" : [ "**/*.test.ts" , "**/*.spec.ts" ],
"exclude" : [ "**/node_modules/**" ],
"body" : [
"describe('${1:suite}', () => {" ,
" \t it('${2:test}', () => {" ,
" \t\t $0" ,
" \t });" ,
"});"
],
"description" : "Test suite"
}
}
Snippet File Locations
User Snippets:
Windows: %APPDATA%\Code\User\snippets\
macOS: ~/Library/Application Support/Code/User/snippets/
Linux: ~/.config/Code/User/snippets/
Language-Specific: [language].json
Global Snippets: *.code-snippets
Global snippets files (.code-snippets) can include snippets for multiple languages and are great for sharing snippets across your team via version control.
Sharing Snippets
To share snippets with your team:
Create a .code-snippets file in your project’s .vscode folder
Commit it to version control
Team members will automatically get these snippets when they open the project
.vscode/project.code-snippets
{
"Project Header" : {
"scope" : "typescript,javascript" ,
"prefix" : "header" ,
"body" : [
"/**" ,
" * @file $TM_FILENAME" ,
" * @project ${WORKSPACE_NAME}" ,
" * @author $1" ,
" */" ,
"$0"
]
}
}
Snippets with TM_SELECTED_TEXT variable only work when text is selected before triggering the snippet.
Troubleshooting
If snippets aren’t working:
Check that the language scope is correct
Verify JSON syntax is valid (no trailing commas)
Ensure the prefix doesn’t conflict with existing snippets
Check that IntelliSense is enabled in settings
Try reloading VS Code window
Next Steps
Settings Configure snippet behavior
Keybindings Add shortcuts for snippet commands