VsCode-创建文件时触发代码段 [英] VsCode - Triggering a snippet upon file creation

查看:123
本文介绍了VsCode-创建文件时触发代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在vscode中创建文件时是否可以触发用户定义的自定义代码段。



我最近开始学习Golang并注意到那里是一些创建 main.go 文件的样板代码。



所以我为此创建了自定义代码段现在,我可以手动触发该代码段并为我节省一些键入击键的操作。



我想更进一步,这样无论何时我创建一个名为 main.go 在VsCode中,它应该自动触发该代码段并为我插入biolerplate代码,而无需我手动触发该代码段。



这甚至可能吗?

解决方案

查看此扩展名:






也请参见此扩展名 https://marketplace.visualstudio.com/items?itemName=bam.vscode-file-templates


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 ?

解决方案

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

这篇关于VsCode-创建文件时触发代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆