- vscode中按“ctrl+shift+p”调出搜索栏,在搜索栏中输入snippet,点击“代码片段:配置用户代码片段”
- 点击后,会显示如下列表,我们要设置是的Python,因此需要点击python.json,(第一次设置时python.json的位置在下拉列表按字母排序“p"的位置,设置过一次后,python.json会跑到下拉列表上方。)
- 打开python.json文件后, 会有如下代码显示
{
// Place your snippets for python 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"
// }
}
复制其中的
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
用ctrl+/快捷键取消每行的注释。
比如要通过输入"mmain"自动补全if name==“main:”,并将鼠标光标另起一行,按下方修改相应的代码内容。
"mmain": {
"prefix": "mmain",//定义的快捷键mmain
"body": [
"if __name__==\"__main__\":", //自动补全的内容,因为我这里有双引号,要用转义字符"\"
"$0" //补全后的光标位置,如果这里没有"$0",自动补全后的光标位置会在上一行的末尾
],
"description": "if __name__==\"__main__\": " //描述内容,因为我这里有双引号,要用转义字符"\"
}
- 完成后的内容如下,多条自动补全命令只要重复以上操作,加到前一条的“}”的后面
{
// Place your snippets for python 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"
// }
"mmain": {
"prefix": "mmain",
"body": [
"if __name__==\"__main__\":",
"$0"
],
"description": "if __name__==\"__main__\": "
}
}