VScode自动添加指定内容

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


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

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

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

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

    • 选择目标语言(例如 Python、JavaScript),或创建全局片段(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 后按 Tab 或 Enter,即可插入模板。

方法 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 扩展。
相关推荐
机智阳1 小时前
系统优化(高级)
linux·运维·编辑器
Answer1st2 小时前
【FPGA学习】VSCode 写 Verilog 操作指南(XII)
vscode·学习·fpga开发
qinglongcn5 小时前
2026 Agent 记忆安全威胁全景
网络·人工智能·安全·web安全·编辑器·ai编程
IT利刃出鞘1 天前
WordPress插件--经典编辑器
编辑器
weixin_449173651 天前
vscode附加到已经有的进程
ide·vscode·编辑器
bkspiderx3 天前
在Visual Studio中安装与配置Qt插件(Qt Visual Studio Tools)
ide·qt·visual studio·qt插件
lusklusklusk4 天前
使用Visual Studio一步步创建并部署SSRS_PBIRS报表:数据源配置、图表设计、参数化查询、部署参数与排错实战
数据库·ide·visual studio
解道Jdon4 天前
Rust重写OpenCode Rune:性能提升最高100倍
ide·windows·git·svn·eclipse·github·visual studio
莫卢俊德4 天前
vscode ssh config
ide·vscode·ssh
郝学胜-神的一滴5 天前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl