VSCode中创建自定义的代码片段,请按照以下步骤操作:
-
打开VSCode,并使用快捷键
Ctrl + Shift + P
打开命令面板。 -
在命令面板中,输入
snippets
并选择Preferences: Configure User Snippets
(首次使用时,你可能需要选择语言,例如“Preferences: Configure User Snippets for JavaScript”)。 -
这将打开一个以所选语言命名的代码片段文件。如果你想为多个语言创建代码片段,请选择
New Global Snippets File
(全局代码片段文件)。 -
在代码片段文件中,你将看到一个空的JSON对象。这是用于定义代码片段的地方。
参考图片动画,复制粘贴 cpp.json
示例模版数据
{
// C:\Users\vip\AppData\Roaming\Code\User\snippets
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
"regex模版": { "prefix": "regex",
"body": [
"#include <bits/stdc++.h>\nusing namespace std;\nint main() {\n string s(\"A1234B5678C999 A1234B5678C999\");"
"\tsmatch m; regex e(\"\\\\\\d+\");\n\n while (regex_search(s, m, e)) {\n cout << m.str() << endl;"
"\t\ts = m.suffix().str();\n }\n}"
], "description": "regex模版" },
"fopen示例模版": { "prefix": "fopen",
"body": [
"#include <stdio.h>\nint main() {\n FILE *pFile;\n pFile = fopen(\"myfile.txt\", \"w\");\n if (pFile != NULL) {",
" $1fputs(\"fopen example\", pFile);\n fclose(pFile);\n }\n return 0;\n}"
], "description": "fopen示例模版" },
"printf模版": { "prefix": "prf",
"body": [
"printf(\"%d\", $1);"
], "description": "printf模版" },
"C语言模版": { "prefix": "cc",
"body": [
"#include <stdio.h>\n#include <stdlib.h>\n",
"int main()",
"{",
"\t$1printf(\"Hello world!\");",
"\treturn 0;",
"}"
], "description": "C语言模版" },
"CPP语言模版": { "prefix": "cpp",
"body": [
"#include <iostream>\n#include <string>\n#include <algorithm>\n",
"using namespace std;",
"int main(int argc, char* argv[])",
"{",
"\t$1cout << \"Hello world!\" << endl;",
"\treturn 0;",
"}" ], "description": "CPP语言模版" }
}
使用 snippets
缩写词代码片段自动插入代码模版
- 输入
cpp
后然后按下回车键,就能插入 cpp代码模版 - 输入
cc
能插入 C语言代码模版 - 输入
regex
能插入 regex代码模版
0 条评论