尽管 AI 编程助手已经无处不在,但总有些常用的个人代码片段是 AI 难以补全的,因此借助 vscode 的 Snippets 的功能配置相应代码片段进行快捷输入还是必要的。
例如这里是我常用的 调试 python 的代码。将其配置为 vscode 的 Snippets 的方法如下:
Ctrl+Shift+P打开设置栏, 搜索Snippets: Configure Snippets。- 在
python.json中配置以下代码:json"python debug preamble": { "prefix": "debugpy", "body": [ "import debugpy", "try:", " debugpy.listen(('localhost', ${1:9501}))", " print('Waiting for debugger attach')", " debugpy.wait_for_client()", "except Exception as e:", " pass", ], "description": "python debug preamble" } - 相应的在
jsonc.json(注意不是json.json)中配置以下代码:json"python debugger setting":{ "prefix": "debugpy", "body": [ "{", " \"name\": \"Debug ${1:example}.py\",", " \"type\": \"debugpy\",", " \"request\": \"attach\",", " \"connect\": {", " \"host\": \"localhost\",", " \"port\": ${2:9501}", " },", " \"justMyCode\": ${3:false}", "}" ], "description": "debugpy setting" }
这样在 .py 文件中输入前缀 debugpy 就会触发自动补全 2 中的代码体的选项,实现快捷输入,同理在 .vscode/launch.json 文件中输入 debugpy 则可以快捷输入 3 中配置的代码。 此外 ${1:9501} 意为在快捷输入后光标首先移动到这个编辑点,从而可以修改监听端口号,以此类推。