How to add snippet to Visual Studio Code for multiple languages?
Asked Answered
P

1

16

I have couple of snippets that shared between js, jsx and html files. Now I have the same snippet code in 3 files. Is it possible to create snippet and specify to what files/types it should be applicable?

Palmy answered 28/11, 2017 at 15:9 Comment(2)
github.com/Microsoft/vscode/issues/13182Drab
Specifically github.com/Microsoft/vscode/issues/13182#issuecomment-355301995Iyeyasu
T
20

Hit cmd + p to access the command bar, then type 'snippets' and select Preferences: Configure User Snippets, then New Global Snippets file. Once you name your file, it will create a global snippets file where you can define snippets that are accessible in any language defined on the scope property.

"Div Tag": {
  "scope": "javascript,javascriptreact,html",
  "prefix": "div",
  "body": "<div>$1</div>",
  "description": "Create a div tag"
}

UPDATE

Fixed the scope property with the correct language identifiers in response to the comments.

Torquemada answered 25/1, 2019 at 21:56 Comment(2)
The file terminations js,jsx did not work in my case. Instead I used "scope": "javascript,javascriptreact" to get the snippets to work in .js and .jsx files. Check this answer: https://mcmap.net/q/418942/-vscode-user-snippet-doesn-39-t-works-inside-jsxSpandau
@Spandau and a list of all ids at: code.visualstudio.com/docs/languages/identifiersThundering

© 2022 - 2024 — McMap. All rights reserved.