How to add common language snippets in Visual Stud

2020-07-05 08:48发布

问题:

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?

回答1:

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).



回答2:

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
}