How to add common language snippets in Visual Studio Code?
Asked Answered
A

2

11

In official vscode documentation i have seen that is possible to create custom snippets for each language. https://code.visualstudio.com/Docs/customization/userdefinedsnippets

Ex. (languageId).json

But if i want to define snippets common for all languages? It is possible?

Algetic answered 2/12, 2016 at 15:31 Comment(0)
P
9

Common users snippets are currently not supported, but there is a VSCode issue tracking this feature request. Please let us know if this is something you would find useful.


For completeness, VSCode extensions can register the same snippets for multiple languages but they must explicitly specify all languages they provide snippets for (there is no "language": "*" option).

Peeve answered 2/12, 2016 at 22:52 Comment(2)
Matt, I'm having trouble finding exactly how to implement the 2nd part of your answer. I'm using the html boilerplate extension, here: github.com/sidthesloth92/vsc_html5_boilerplate and have modified the snippets section in the package.json to add another reference that specifies "php" for the language and points the same snippets file (and restarted VS Code), to no avail. Any clues?Welldefined
Never mind.. I realise now that I needed to use "blade" as the language, as I had added support for thatWelldefined
P
0

For completeness, global snippets are now supported in VS Code out the box.

Probably redundant information as the UI already guides you to it via the auto-generated global.code-snippets file when you open User Snippets in the menu.

However something I didn't know and landed on this answer looking to find is that you can also use the scope field to narrow global snippets to a specific subset of languages.

For example if I want a certain snippet available in both javascript and typescript, but only in javascript and typescript (because it's annoying if it also appears in other languages)

"My Snippet": {
  "prefix": "my-snippet",
  "body": [ "my snippet code..." ],
  "scope": "javascript,typescript" // define a strict subset of languages here
}
Phillisphilly answered 8/10, 2019 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.