πŸ’‘ VS Code β€” Code Snippets Cheatsheet


🧭 Introduction

VS Code supports two complementary systems for automation:

  1. User Snippets β€” dynamic, context-aware pieces of code or text that you trigger manually inside existing files. They live in JSON files and expand when you type their prefix and hit Tab.

  2. File Templates (via Extensions or Snippet Files) β€” prefilled structures for new files, often provided by extensions like Project Templates or File Templates Generator. These use the same variable system but apply automatically when you create a file.

Together, they make VS Code behave like a lightweight IDE: boilerplate vanishes, consistency remains.


🧩 PART 1 β€” User Snippets


βš™οΈ What User Snippets Are

User snippets are stored in JSON files per language (or globally). Each snippet includes a prefix, body, and description. Variables like TM_FILENAME or CURRENT_YEAR can be embedded directly in the body.

Path: File β†’ Preferences β†’ User Snippets β†’ choose language or New Global Snippets file…


🧱 Example β€” Markdown Front Matter with Auto Date

File: markdown.json

{
  "YAML Front Matter": {
    "prefix": "yfm",
    "description": "YAML front matter for Markdown",
    "body": [
      "---",
      "title: ${1:title}",
      "date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}",
      "tags:",
      "summary: ${2:summary}",
      "aliases:",
      "---",
      "$0"
    ]
  }
}

Usage: Type yfm β†’ Tab β†’ VS Code expands to:

---
title: My Note
date: 2025-10-17
tags:
summary: Short intro
aliases:
---

⚑ Triggering Snippets

Action Shortcut Description
Expand snippet Tab Type prefix and press Tab
Show available snippets Ctrl + Space (Win/Linux) / ⌘ Space (macOS) Lists snippets valid for the current file
Edit snippets Ctrl + Shift + P β†’ Configure User Snippets Opens snippet files

🧰 Common VS Code Snippet Variables

Variable Example Output Description
$CURRENT_YEAR 2025 Current year
$CURRENT_MONTH 10 Current month
$CURRENT_DATE 17 Current day
$CURRENT_HOUR 14 Current hour
$CURRENT_MINUTE 32 Current minute
$CURRENT_SECOND 07 Current second
$TM_FILENAME UserService.java File name
$TM_FILENAME_BASE UserService File name without extension
$TM_LINE_NUMBER 42 Current line number
$TM_SELECTED_TEXT (selected code) Selected text
$TM_CURRENT_WORD (word under cursor) Current word
$CLIPBOARD (clipboard text) Clipboard contents
$RANDOM random integer Random value
$UUID 2a4e... Unique identifier
$BLOCK_COMMENT_START /* Language’s block comment start
$BLOCK_COMMENT_END */ Language’s block comment end
$TM_DIRECTORY /home/user/project/src Directory of the current file
$RELATIVE_FILEPATH src/app.js Relative path from workspace root
$WORKSPACE_NAME my-project Workspace folder name
${1:placeholder} cursor tabstop Field for manual input
$0 β€” Final cursor position

Pro tip: You can nest variables: "${TM_FILENAME_BASE}_${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}" β†’ UserService_2025-10-17


πŸ’‘ Power Tips for Snippets

  • Add tab stops ${1}, ${2} … and a final $0 to control cursor flow.
  • Use ${CLIPBOARD} to instantly insert what’s copied.
  • For optional defaults: ${1:defaultValue} β†’ replaced when you type.
  • Multi-line snippets use an array of strings β€” each line quoted.
  • Use language-specific snippet files (markdown.json, java.json, etc.) to scope snippets.

πŸ—οΈ PART 2 β€” File Templates (Projects and New Files)


βš™οΈ What File Templates Are

VS Code doesn’t have a native β€œFile Template” system like IntelliJ, but several extensions provide similar behavior:

  • Project Templates by zardoy β€” create project/file blueprints.
  • Snippet Templates or Advanced New File β€” insert predefined content on file creation.
  • Or simply use a global snippet with the command Insert Snippet (Ctrl + Shift + P).

All use the same variables as normal snippets.


🧱 Example β€” Markdown Note Template

Using the same YAML header pattern for new Markdown files:

{
  "New MD Note": {
    "prefix": "newnote",
    "description": "Prefilled Markdown note structure",
    "body": [
      "---",
      "title: ${TM_FILENAME_BASE}",
      "date: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE} ${CURRENT_HOUR}:${CURRENT_MINUTE}",
      "tags:",
      "summary:",
      "aliases:",
      "---",
      "",
      "$0"
    ]
  }
}

Usage: Type newnote in a new empty file β†’ Tab β†’ prefilled note.


πŸ“˜ Common Patterns for Templates

  • Timestamps: ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}_${CURRENT_HOUR}${CURRENT_MINUTE}
  • User header: Author: ${USER} (works if environment variable set)
  • Filename metadata: File: ${TM_FILENAME}
  • Unique ID: id: ${UUID}

🧭 Quick Reference

System Purpose Syntax Trigger
User Snippet Expand snippet inside existing file ${VARIABLE} Prefix + Tab
File Template (via extension) Prefilled new file content ${VARIABLE} New File command / snippet
Multi-cursor / Selection Insert across many lines $TM_SELECTED_TEXT Works with multi-cursor
Clipboard Insert Paste clipboard text $CLIPBOARD Inside snippet body

🧠 Summary

User Snippets β†’ dynamic, context-aware expansions inside existing files. Use VS Code variables like $CURRENT_YEAR, $TM_FILENAME_BASE, $UUID, and $CLIPBOARD to insert live data.

File Templates β†’ prefilled structures for new files, implemented via extensions or global snippets. Use the same ${VARIABLE} syntax for consistency.

Both systems integrate seamlessly β€” create once, reuse forever. VS Code’s JSON-based approach makes snippets portable, version-controlled, and easy to share across machines.