vscode配置自定义代码片段:让vscode具有和IDEA一样的代码段生成快捷键(sout或psvm)的配置方法为例说明/自定义html代码片段

本文介绍如何在VSCode中配置代码片段以提高编程效率,包括配置入口、配置原理及Java和HTML片段示例。

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

官方文档

优先查阅文档
包含更多的信息

打开配置入口

ctrl+shift+p配置

在这里插入图片描述
输入preferences:configure user Snippets
(或者只输入snippet)

配置基本原理

事实上几乎所有语言的snippet套路是一样的,都是将设计好的代码模板(初步设计的代码可能就是可以运行的一段代码片段)
将其中的每一行用引号包围起来(如果语句本身含有引号,那么请使用反斜杠来转转义它),这些被包装成字符串的语句(乃至符号/空行)都将分别作为json中的body:属性的值(数组值)中的一个元素

java.json (实例1)

再输入java,点击确认,编辑java.json
在这里插入图片描述
在这里插入图片描述

  • 也可以直接按路径找到java.json
  • 根据文件中的注释内容给出的json说明,可以自行配置

sout/psvm配置样例

Java.json里面的内容用如下代码覆盖(比如:添加sout,psvm的补全效果)

{
	// Place your snippets for java 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"
	// }
	"main 代码段": {
		"prefix": "psvm",
		"body": [
			"public static void main(String[] args){",
			"\t$1",
			"}"
		],
		"description": "main代码段"
	},
	"print": {
		"prefix": "sout",
		"body": [
			"System.out.println(\"$1\");"
			// "${1:}",
			// ")"
		],
		"description": "System.out.println"
	}
}

具体的代码为:(可以不必深究)

"main 代码段": {
		"prefix": "psvm",
		"body": [
			"public static void main(String[] args){",
			"\t$1",
			"}"
		],
		"description": "main代码段"
	},
	"print": {
		"prefix": "sout",
		"body": [
			"System.out.println(\"$1\");"
			// "${1:}",
			// ")"
		],
		"description": "System.out.println"
	}

效果(注意不要太着急enter/tab,不然vscode还没反应过来)

在这里插入图片描述

html 片段实例

{
	// Place your snippets for html 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": {
		//there ,the `print to console" will be the tip shows on the suggestion list 
		// the prefix field there:`log` is the trigger to introduce and insert the code snippet.
		"prefix": "log",
		"body": [
			"console.log('$1');",
			"$2"
		],
		"description": "Log output to console"
	},
	"vue html snippet": {
		"prefix": "vueHtml",
		"body": [
			"<!-- import vue cdn: -->",
			"<script src=\"https://unpkg.com/vue@next\"></script>",
			"<div id=\"app\">",
			"<!-- write your vue tags or common html tages there: -->",
			"</div>",
			"<!-- write your js code: -->",
			"<script>",
			"\tVue.createApp({",
			/* define your variables (which will be bind to tags properties there:) */
			"\tdata(\t){",
			"\t\t$1",
			"\t\t/* separate your variables by comma:`,` */ ",
			"\t\t$2 ",
			"\t},",
			/* write your methods there:(including callbacks and so on) */
			"\tmethods:{",
			"\t\t$3",
			"\t\t/* separate your variables by comma:`,` */ ",
			"\t\t$4",
			"\t}",
			"}).mount(\"#app\")",
			"\t</script>",
			/* */
		],
		"description": "insert the vue basic template."
	}
}

效果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cxxu1375

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

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

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

打赏作者

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

抵扣说明:

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

余额充值