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.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Microsoft/vscode/llms.txt
Use this file to discover all available pages before exploring further.
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.
Viewing Available Snippets
To see all available snippets for the current language:- Open Command Palette (
Ctrl+Shift+PorCmd+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
TypeScript Snippets
HTML Snippets
html document (prefix: html or !)
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
Snippet Syntax
Snippets are defined in JSON with the following structure:javascript.json
Basic Example
javascript.json
Snippet Features
Tabstops
Tabstops allow you to move the cursor through snippet fields:$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:Choice
Provide a dropdown of choices:Variables
Snippets can use predefined 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
Variable Transformations
Transform variable values:Multi-Language Snippets
Global snippets files (.code-snippets) can define snippets for multiple languages:
my-snippets.code-snippets
Advanced Snippet Examples
Try-Catch with Error Handling
javascript.json
Async Function with Error Handling
javascript.json
React Hook
typescriptreact.json
Test Case
typescript.json
File Template Snippets
You can create snippets that are suggested when creating new files:Snippet Scope and Include/Exclude
Control where snippets appear:Snippet File Locations
-
User Snippets:
- Windows:
%APPDATA%\Code\User\snippets\ - macOS:
~/Library/Application Support/Code/User/snippets/ - Linux:
~/.config/Code/User/snippets/
- Windows:
-
Language-Specific:
[language].json -
Global Snippets:
*.code-snippets
Sharing Snippets
To share snippets with your team:- Create a
.code-snippetsfile in your project’s.vscodefolder - Commit it to version control
- Team members will automatically get these snippets when they open the project
.vscode/project.code-snippets
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