How can I create templates for file extensions in Visual Studio Code?

The extension Auto Snippet does exactly that.

You only need to configure two things:

  • The snippet's name
  • A filename pattern or a language for when the snippet should be applied

Recommendation

The author has some very custom defaults, so as soon as you install it, modify its settings and remove those patterns that you won't need.

Otherwise, it will complain every time you create a file and doesn't find the snippet configured.


There isn't, not natively. But there is an extension called File Templates for VSCode that allows you to create your own file templates and generate from them. But I think you'd benefit from making an extension to do just that and maybe even more.

In the meantime, you can use a snippet to generate this instead of having to copy paste.

Go to File > Preferences > User Snippets and choose Vue from the dropdown. Vue will only show up if you have installed an extension that supports this file type. In this case, I'd recommend Vetur, but you probably have it already.

Then just add this entry to your vue.json file:

"vuetpl" : {
        "body": [
            "<template>",
            "\t<div>",
            "\t\t$0",
            "\t</div>",
            "</template>",

            "<script>",

            "export default{",
            "\tdata(){",
                "\t\treturn{",
                    "\t\t\t",
                "\t\t}",
            "\t},",
            "\tmethods:{",
                "\t\t",
            "\t},",
            "\tcreated(){",
                "\t\t",
            "\t}",
            "}",
            "</script>",

            "<style scoped>",
            "</style>",
        ],
        "prefix": "vuetpl",
        "description": "Creates a new template."
    }

Then, when you create a new .vue file, just type vuetpl and press tab to autocomplete, and you'll have this:

Template

Of course, you can also use a Snippet Generator to make your own snippets.


This is being worked on now and is built-in to vscode v1.70 Insiders and may be in Stable v1.70 early August, 2022.

1. Make sure you haven't disabled this setting by setting it to hidden:

// Controls if the untitled text hint should be visible in the editor.  
"workbench.editor.untitled.hint": "text",   // "text" is the default

2. Make some snippets that will serve as templates for whatever languages you are interested in in a snippets file (here they are in a global snippets file):

"vue template": {
    "isFileTemplate": true,  // changing to this soon
    "isTopLevel": true,  // was this
    "scope": "vue",

    "prefix": "vueTemplate",
    "body": [
        "const a = 'vue template'"
    ],
    "description": "vue template"
},

"javascript template": {
    "isFileTemplate": true,
    "scope": "javascript",

    "prefix": "jsTemplate",
    "body": [
        "const a = 'javascript template'"
    ],
    "description": "javascript template"
},

The isFileTemplate option is key here. Any snippet with this option will appear in the following workflows.

If you have the "scope": "someLangID here" set in the keybinding then vscode can and will automatically change the current editor's language to that language ID.

3. Create a new file:

a. with the command File: New Untitled File Ctrl+N

[the following option in the new file message start with snippet has been delayed and won't be in v1.70 Stable - the command SNippets: Populate From Snippet is working though.]

Then you will see the message at the top of the file as in this demo:

start with snippet

Clicking on that will show any snippets with that "isFileTemplate": true, set. Choosing one from the resulting QuickPick thaht opens up will input the snippet contents AND change the editor's language association to the scope value.

snippet templates for a new file

b. You can also modify an existing file to the snippet content and language by using the command (found in the Command Palette)
Snippets: Populate from Snippet

[This command workbench.action.populateFileFromSNippet does not have a default keybinding.]

As you can see in the demo, using this command will delete all the current contents of the file AND change the language association of that editor.

snippet template with an existing file


So making your initial snippets will probably be the hardest part, you might look into the snippet generator app.