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

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.

Leave a Comment