Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>Snippet on File TypeNew to Visual Studio Code? Get it now.
Snippet on File Type

Snippet on File Type

zenpk

|
23 installs
| (0) | Free
Insert different code snippets based on the file type
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Snippet on File Type

A VSCode extension for inserting different code snippets based on the file type. Works well with shortcuts and Vim.

Setup

Define snippets in settings.json

{
  "snippet-on-file-type.defineSnippets": [
    {
      "fileTypes": [
        "javascript",
        "javascriptreact",
        "typescript",
        "typescriptreact"
      ],
      "snippets": {
        // $1 is where the cursor stops, you can also add $2, $3...
        "print": ["console.log($1);"],
        "log": ["console.log(`========== $1 ==========`);"],
        "error": [
          "try {",
          "  $1",
          "} catch (e) {",
          "  console.log(e);",
          "}"
        ]
      }
    },
    {
      "fileTypes": ["go"],
      "snippets": {
        "print": ["fmt.Println($1)"],
        "log": ["log.Println(\"========== $1 ==========\")"],
        "error": [
          "if err != nil {",
          "    return $1, err",
          "}",
          "" // empty string at the end for adding a new line
        ]
      }
    }
  ]
}

Usage

1. In keybindings.json

{
  "key": "ctrl+alt+p",
  "command": "snippet-on-file-type.insertSnippetBasedOnFileType",
  // false: insert the snippet at the cursor position instead of a new line
  "args": ["print", false]
}

2. In settings.json, with Vim (recommended)

{
  "vim.normalModeKeyBindingsNonRecursive": [
    {
      "before": ["<leader>", "g", "p"], // [g]enerate [p]rint
      "commands": [
        {
          "command": "snippet-on-file-type.insertSnippetBasedOnFileType",
          "args": ["print"]
        }
      ]
    },
    {
      "before": ["<leader>", "g", "l"], // [g]enerate [l]og
      "commands": [
        {
          "command": "snippet-on-file-type.insertSnippetBasedOnFileType",
          "args": ["log"]
        }
      ]
    }
  ]
}

Why not built-in User Snippets?

The built-in User Snippets feature doesn't support shortcut configuration, making it hard to integrate with Vim. If the snippet's prefix is too short, it appears too often at the top of tab completion. If it's too long, it loses the benefit of being a "shortcut".

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft