VScode自定义代码片段

51 阅读1分钟

VScode自定义代码片段

1、创建代码片段文件

1 2

2、编写代码片段

{
	// Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
	// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
	// is left empty or omitted, the snippet gets applied to all languages. 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.
	"Print to console": {
		"prefix": "wblog",
		"body": [
			"console.log('$1');"
		],
		"description": "快速打印"
	},
	"Print to node": {     //代码段名称
		"prefix": "wbnode",         //代码段前缀
        "body": [
			"{/*——————————————————————————————$1start——————————————————————————————*/}",   //代码段  $1表示光标插入的位置
		    "{/*——————————————————————————————$2end——————————————————————————————*/}"
		],
		"description": "分割占位" // 描述
	}
}

3、代码片段解读

prefix      :代码片段名字,即输入此名字就可以调用代码片段。
body        :这个是代码段的主体.需要编写的代码放在这里,      
$1          :生成代码后光标的初始位置.
$2          :生成代码后光标的第二个位置,按tab键可进行快速切换,还可以有$3,$4,$5.....
${1,字符}    :生成代码后光标的初始位置(其中1表示光标开始的序号,字符表示生成代码后光标会直接选中字符。)
description :代码段描述,输入名字后编辑器显示的提示信息。


附上生成代码片段地址: 生成代码片段