VS CODE自定义代码片段

前提

VS CODE是真的强大!不是我觉得你LOW,是我根本不了解你的强大之处。差距就在于,你已经做到了,而我要么是没想到,要么是想到了却无知的认为你不具备这样的功能。

用户代码片段(User snippet)的主要功能在于可以提前把经常用到的一些代码片段进行预先编写好。在实际编程中可以实现高效快速插入的目的。

进入用户代码片段(User snippet)
在这里插入图片描述
在这里插入图片描述

一、快速写注释

在打开的 matlab.json 中添加一下片段,绿色为新添加的。注意格式,花括号内的是对象,以 “Var” = “key” 的形式表示,[]括号里面为数组。彼此以逗号分隔,且这里每一行" "表示输出的一行中的内容。$CURRENT_YEAR等是特殊关键字,可以在参考链接中查询。

{
	// Place your snippets for matlab 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"
	// }
	"head comment": {
		"prefix": "hc",
		"body": [
			"% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % ",
			"% Aim: $1",
			"% Date: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
			"% Author: Dayang Wang, SYSU",
			"% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % ",
			"$0"
		]
	}
}

其中,$0,$1表示代码执行之后,光标落在的位置,通过TAB键,可以实现下一处的光标转换。

保存之后,可以尝试新建一个.m脚本,然后输入hc, 直接回车,这段注释就随即插入了。

二、自定义代码片段

自定义代码片段的方式和注释是一致的,内容不同而已。这里以经常用到的pcolor为例子进行说明:

{
	// Place your snippets for matlab 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"
	// }
	"head comment": {
		"prefix": "hc",
		"body": [
			"% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % ",
			"% Aim: $1",
			"% Date: $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
			"% Author: Dayang Wang, Sun Yat-sen University",
			"% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % ",
			"$0"
		],
	},
	"pcolor_snp":{
		"prefix": "pp",
		"body": [
			"h = pcolor(${0:Varname});",
			"h.LineStyle:'none';"
		]
	}
}
  • 通过 ${0:Varname} 的形式,可以对该处需要输入的内容进行描述;
  • 通过 ${0|Varname} 的形式,可以对该处需要输入的内容提供选项;

参考网站

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大雨海深

感谢您的支持和鼓励

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

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

打赏作者

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

抵扣说明:

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

余额充值