Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

In my Vue.js projects almost all the times I need this code snippet as a template.

<template>     <div>     </div> <template>  <script>  export default{     data(){         return{         }     },     methods:{     },     created(){     } } </script>  <style scoped> </style> 

Is there a way to tell Visual Studio Code each and every time I create a file with the extension .vue to automatically add that snippet to the file?

Simply, when I create new file with a certain extension, the predefined template for that extension should automatically be added to the file.

like image 335
Pathum Kalhan Avatar asked May 28 '18 17:05

Pathum Kalhan


People also ask

How do I create a VS Code template?

In VSCode, open a folder that will contain your new project. Use the Command Palette to execute the command "Project: Create Project From Template". A list of available templates should appear. Select the desired template.

Where is the Visual Studio code template?

Templates can be accessed through search (Ctrl+Q) and in the “New Project Dialog” (Ctrl+Shift+N). Both entry points will also be enabled with fuzzy search (to help automatically rectify typos), highlighted matches to your search query in the results, and improved ranking to ensure increased accuracy.


2 Answers

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.

like image 105
Phiter Avatar answered Oct 03 '22 00:10

Phiter


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.

like image 31
Madacol Avatar answered Oct 02 '22 23:10

Madacol