VScode自动添加指定内容

在 VS Code 中,可以通过配置 用户代码片段(User Snippets) 或使用 文件模板扩展 来实现新建指定文件类型时自动添加指定内容。以下是具体方法:


方法 1:使用 VS Code 内置的「用户代码片段」

适用场景 :适用于特定文件类型(如 .js.py.html 等),在新建文件后通过快捷键插入预设内容。

步骤
  1. 打开代码片段配置

    • 快捷键:Ctrl + Shift + P(Windows/Linux)或 Cmd + Shift + P(Mac),输入 Configure User Snippets

    • 选择目标语言(例如 PythonJavaScript),或创建全局片段(New Global Snippets file)。

    • 我需要配置Verilog的默认内容,要选择verilog.json文件配置

  2. 编辑代码片段

    • 例如为 .v 文件添加默认内容,编辑 verilog.json
json 复制代码
		{
	// Place your snippets for verilog 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"
	// }
	"File description": {
		"prefix": "file",
		"body": [
			"/*******************************************************************************",
			"Date:${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}",
			"Author: Ecoboy",
			"Ecoding: UTF-8",
			"Version: 1.0",
			"Description:",
			"*********************************************************************************/",
				],
		"description": "File description comment"
	},

	"block parameter description": {
		"prefix": "para",
		"body": [
				"//========================================================================\\",
				"// =========== Define Parameter and Internal signals ===========" ,
				"//========================================================================/",
				],
		"description": "Block description comment"
	},


	"block main code description": {
		"prefix": "maincode",
		"body": [
				"//=============================================================================",
				"//**************    Main Code   **************",
				"//============================================================================="
				],
		"description": "Block description comment"
	},

}
  • 变量说明
    • ${1:占位文本}:光标跳转位置,按 Tab 切换。
    • $CURRENT_YEAR 等:VS Code 内置变量(自动填充日期/时间)。
  1. 使用代码片段
    • 新建 .py 文件,输入 py_template 后按 TabEnter,即可插入模板。

方法 2:使用扩展实现「新建文件时自动填充」

适用场景:希望在创建文件时(如右键新建)自动填充内容,无需手动触发代码片段。

推荐扩展
  1. File Templates (扩展ID:brpaz.file-templates

    • 安装后,通过命令面板(Ctrl+Shift+P)运行 File Templates: Create File from Template
    • 配置模板文件(支持变量如 ${date}${name})。
  2. Template Generator (扩展ID:krizzdewizz.template-generator

    • 支持更复杂的模板逻辑,如根据目录生成不同内容。
配置示例(以 File Templates 为例)
  1. 安装扩展后,在项目根目录创建 .vscode/templates 文件夹。

  2. 新建模板文件(如 template.py)并写入内容:

    python 复制代码
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    """
    Description: ${1:Description}
    Author: ${2:Your Name}
    """
    
    def main():
        ${0:pass}
    
    if __name__ == "__main__":
        main()
  3. 通过命令面板选择模板创建文件。


方法 3:自定义脚本(高级)

如果需要更复杂的逻辑(如根据项目类型动态生成内容),可以:

  1. 编写一个 Node.js/Python 脚本 生成文件。
  2. 通过 VS Code 的 Task扩展 API 绑定到快捷键/右键菜单。

总结

方法 优点 缺点
用户代码片段 原生支持,无需扩展 需手动触发(输入前缀)
文件模板扩展 自动化强,支持变量 需安装扩展
自定义脚本 灵活性最高 配置复杂

推荐

  • 简单场景 → 用户代码片段
  • 全自动填充 → File Templates 扩展
相关推荐
allnlei5 小时前
使用CLion进行远程开发(Remote Development)
ide·1024程序员节
zandy10115 小时前
2025年AI IDE的深度评测与推荐:从单一功能效率转向生态壁垒
ide·人工智能
来块小鱼饼干(≧^.^≦)10 小时前
教你如何使用VSCode的EIDE插件开发STM32(包括任何ARM内核芯片)最详细教程
ide·vscode·stm32·arm·keil
请叫我欧皇i12 小时前
保姆级教程vscode创建uniapp vue3+ts+pinia项目并实现自动导入、打包功能
ide·vscode·uni-app
应用市场17 小时前
VSCode + AI Agent实现直接编译调试:告别Visual Studio的原理与实践
人工智能·vscode·visual studio
明道源码20 小时前
Android Studio 创建 Android 模拟器
android·ide·android studio
明道源码21 小时前
Android Studio 使用教程
android·ide·android studio
Caesar Zou1 天前
ssh远程连接服务器,vscode不显示claude
服务器·vscode·ssh
JoannaJuanCV1 天前
vscode debug Transformer源码说明
ide·vscode·transformer·qwen3
炘东5921 天前
让Pycharm的Terminal(终端)进入创建好的虚拟环境
ide·python·pycharm