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 扩展
相关推荐
Java斌40 分钟前
70年使用权的IntelliJ IDEA Ultimate安装教程
java·ide·intellij-idea
weixin_466485111 小时前
PyCharm中运行.py脚本程序
ide·python·pycharm
程序猿小D1 小时前
第14节 Node.js 全局对象
linux·前端·npm·node.js·编辑器·vim
aningxiaoxixi1 小时前
Android Studio 之基础代码解析
android·ide·android studio
waterHBO2 小时前
一个小小的 flask app, 几个小工具,拼凑一下
javascript·vscode·python·flask·web app·agent mode·vibe coding
界面开发小八哥3 小时前
「Java EE开发指南」如何使用MyEclipse在Web项目中用Web Fragments?
java·前端·ide·java-ee·eclipse·myeclipse
咖啡续命又一天5 小时前
nano编辑器的详细使用教程
编辑器
伍六星5 小时前
更新Java的环境变量后VScode/cursor里面还是之前的环境变量
java·开发语言·vscode
西域曼波王11 小时前
哈尔滨工业大学计算机系统大作业程序人生-Hello’s P2P
linux·c语言·编辑器·vim
LAOning1114 小时前
xcode 各版本真机调试包下载
ide·macos·xcode