前提
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} 的形式,可以对该处需要输入的内容提供选项;
参考网站
- VS code website
https://code.visualstudio.com/- 自动生成user snippet 网站
https://snippet-generator.app/- 特殊字符串查询网站
https://code.visualstudio.com/docs/editor/userdefinedsnippets- vs code user snippet [程序员阿汤]
https://www.bilibili.com/video/BV1PZ4y1H7SJ