(VS Code) 自定义代码片段 Snippets
操作入口
快捷键
使用快捷键 Ctrl + Shift + P
输入Snippets:Configure User Snippets
导航栏
文件 -> 首选项 -> 配置用户代码片段
片段种类
- 全局代码片段
- 当前文件夹的代码片段
- 指定语言的代码片段
默认信息
{
// Place your snippets for c 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"
// }
}
// 片段名称
"": {
// 触发词
"prefix": "",
// 片段内容
"body": [
""
],
// 片段描述
"description": ""
}
编辑
有占位符的时候,可以通过tab键进行编辑
常规
"快速幂": {
"prefix": "binpow",
"body": [
"int binPow(int base, int expo, int mod) {",
" int ans = 1;",
" base %= mod;",
" if (expo == 0) {",
" ans = 1 % mod;",
" } else {",
" while (expo) {",
" if (expo & 1) {",
" ans = ans * base % mod;",
" }",
" base = base * base % mod;",
" expo >>= 1;",
" }",
" }",
" return ans;",
"};"
],
"description": "快速幂"
}
占位符-无默认
"占位符-无默认": {
"prefix": "forr",
"body": [
" for (int $1 = 0; $1 < $2; $1 += 1) {",
" ",
" }"
],
"description": "占位符-无默认"
}
占位符-有默认
"占位符-有默认": {
"prefix": "forr",
"body": [
" for (int ${1:i} = 0; ${1:i} < ${2:n}; ${1:i} += 1) {",
" ",
" }"
],
"description": "占位符-有默认"
}
辅助工具
snippet generator (snippet-generator.app)