Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VsCode - Triggering a snippet upon file creation

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 ?

like image 745
Rohit Avatar asked Apr 10 '19 13:04

Rohit


Video Answer


1 Answers

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:

demo of auto snippet extension


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

like image 119
Mark Avatar answered Oct 17 '22 11:10

Mark