VsCode - Triggering a snippet upon file creation

╄→гoц情女王★ 提交于 2019-12-24 03:36:18

问题


I wanted to know if it is possible to trigger a user defined custom snippet when I create a file in vscode.

I started learning Golang recently and noticed that there is some boilerplate code that goes into creating the main.go file.

So I created my custom snippet for it and now now I can trigger the snippet manually and save me some keystrokes of typing.

I wanted to go one step further, so that whenever I create a new file named main.go from within VsCode, it should automatically fire that snippet and insert the biolerplate code for me, without me having to manually trigger the snippet.

Is this even possible ?


回答1:


Check out this extension: Auto Snippet:

With this in your settings.json:

"autoSnippet.snippets": [
  { "pattern": "**/main.go", "snippet": "main-go-template" },
],

[Note that the example settings in the extension docs has a few syntax errors - use my example above - I have filed an issue with the creator.]

And then your snippet

"main-go-template": {
  "prefix": "zz",
  "body": [
    ...  // you can use tabstops and transforms just like regular snippets
  ]
  "description": "javascript template"
}

Then creating a new main.go file or opening an empty one should trigger the snippet.

It is a good idea to reload vscode whenever you make a change to this extension's settings.

Here is a demo of a gulpfile with common boilerplate:


Also see this extension https://marketplace.visualstudio.com/items?itemName=bam.vscode-file-templates



来源:https://stackoverflow.com/questions/55614398/vscode-triggering-a-snippet-upon-file-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!