VScode新建自定义模板快捷方式

在VSCode开发中,新建Vue文件需自定义初始化模板。具体步骤为:打开VSCode,通过“文件”“首选项”“用户代码片段”新建全局代码片段文件,复制内容覆盖,保存关闭。新建文件输入对应命令回车即可生成模板,还可根据实际修改参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VS新建vue文件的自定义模板

在使用vscode开发的时候,新建vue文件是不可或缺的,但是VSCode并没有vue文件的初始化模板,这个需要自定义模板。
我们可以使用vscodesnippets在新建.vue 文件后轻松获得一套模板。

具体步骤

  • 打开VSCode -> “文件(mac机选Code)” -> “首选项” -> "用户代码片段”,然后会打开一个弹框。
  • 在弹框中选择"新建代码片段"新建一个全局代码片段文件,自定义名称保存后,VS编辑器就会打开这个文件。
    在这里插入图片描述
  • 然后复制以下内容覆盖文件内容。
{
   // Place your snippets for vue here. Each snippet is defined under a snippet name and has a prefix, body and
   // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
   // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
   // same ids are connected.
   // Example:
   // "Print to console": {
   //  "prefix": "log",
   //  "body": [
   //      "console.log('$1');",
   //      "$2"
   //  ],
   //  "description": "Log output to console"
   // }
   	"Create vue template": {
   	"prefix": "vue",
   	"body": [
   		"<template>",
   		"  <div></div>",
   		"</template>",
   		"<script>",
   		"export default {",
   		"  name: \"${1:component_name}\",",
   		"  data () {",
   		"   return {}",
   		"  },",
   		"   methods:{}",
   		"}",
   		"</script>",
   		"<style lang=\"${2:scss}\" scoped>",
   		"</style>"
   	],
   	"description": "Create vue template"
   }
}

prefix 表示对应生成代码块的命令(我设置的是vue
然后将文件保存关闭。

  • 然后新建一个.vue文件
    在新建的.vue的文件中输入vue回车,然后就会生成初始化代码块。
    在这里插入图片描述
  • 生成初始化模板如图:
    具体参数可以根据实际修改,此处我是用的是scss预处理语言
    在这里插入图片描述

注意:

步骤二还有一个方法(方法二: 在弹框中搜索vue,选中vue.json,在vue.json中编辑自定义模板)

  • 新建全局代码片段的好处就是,在非vue文件中输入vue命令都能新建出vue初始化代码模板。
  • 如果是vue.json文件中定义初始化代码模板的话,就只能在.vue文件中vue命令才会有效。
### 创建 VSCode 中 C 语言文件的代码模板 为了在 Visual Studio Code (VSCode) 中设置用于创建 C 文件的代码模板,可以通过配置 `settings.json` 和使用扩展来实现自定义功能。以下是详细的说明: #### 配置 settings.json 实现新文件默认内容 通过修改用户的全局设置或工作区特定设置中的 `files.templates.c` 属性可以指定新建 `.c` 文件时自动填充的内容。 ```json { "files.templateLanguageOverrides": { "*.c": "c" }, "[c]": { "editor.defaultFormatter": "ms-vscode.cpptools", "editor.codeActionsOnSave": { "source.organizeImports": true } }, "files.trimTrailingWhitespace": true, "files.insertFinalNewline": true, "files.exclude": { "**/*.o": true, "**/*.out": true }, "files.newFileDefaultEncoding": "utf8", "files.saveFilesInActiveGroup": true, "files.autoGuessEncoding": false, "files.associations": { "*.h": "c" } } ``` 上述 JSON 片段设置了多个选项以优化 C 开发体验[^2]。然而要真正定制每次打开新的 .c 文件时显示的具体模板,则需进一步操作。 对于更复杂的初始化需求比如包含版权信息或者函数声明部分的标准布局等场景下推荐利用 snippets 功能完成个性化设定过程如下所示: #### 使用 Snippets 定义复杂模板 进入命令面板 (`Ctrl+Shift+P`) 并输入 “Preferences: Configure User Snippets”,接着选择 `newGlobalSnippetsFile...` 来建立一个新的片段集合命名为如 'ccode' 或者其他容易辨认的名字后缀应为`.json` 。编辑该文档加入下面类似的结构作为基础框架例子 : ```json { "Print to console": { "prefix": ["main"], "body": [ "#include <stdio.h>", "", "int main() {", "\tprintf(\"Hello, world!\\n\");", "\treturn 0;", "}" ], "description": "Create a basic C program with printf statement." } } ``` 这样当键入前缀(main),就会触发展开成完整的主程序样板[^3]。 另外值得注意的是如果希望这些预设能够自动化应用而无需手动调用的话还可以考虑安装一些插件例如 [Auto File Header](https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring) 这类工具可以帮助我们达成目的同时保持灵活性以便适应不同项目的要求变化情况下的调整需要[^4]。 ```python def create_c_file_template(): """ This function demonstrates how you might programmatically generate or manage templates within your development environment. Note this is illustrative and not directly executable code inside vscode context. """ template_content = """\ #include <stdio.h> // Function declarations go here... int main(){ // Your initial logic goes here... return 0; }""" return template_content ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yusirxiaer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值