Change the snippets location in Visual Studio Code

2019-05-03 19:16发布

问题:

I am using visual studio code, and need to place the user snippets in my project repository. How can I change the location of the snippets file to a custom one? I am working on windows 10.

回答1:

Update: project level snippets are now a built-in feature as of the September 2018 release, so this no longer requires a third-party extension.


There is currently an open feature request for this (#8102), but you can already get this functionality by using the Project Snippets extension - it lets you place snippets in:

.vscode/snippets/<language>.json


回答2:

[added as an answer because really too long for a comment, just adding the details from the updated link in @Gama11's answer above.]

Per the September 2018 update mentioned above:

Ctrl-Shift-P, choose "Preferences: Configure User Snippets",

then you should see an option like: "New Snippets for [your workspaceFolder name here]…" Choose that and it will create a global snippets file in the .vscode folder.

So as long as you have that folder under source control you are set. Although it is a global snippets file, you can limit the scope of each snippet like so:

{
  "prefix": myPrefix,
  "body": "...",
  "description": "some description",
  "scope": "javascript,typescript"
}

Now those scoped snippet prefixes will not show (as suggestions) in all file types.