目的
在VScode中定义代码片段,方便在markdown、RMarkdown、Quarto中快速输入Python或R的代码片段头,如下形式:
```{python}
光标在这里
代码片段
deepseek给出了代码片段、快捷键两种方式。如下是代码片段的形式
{
"Insert Python Code Block": {
"prefix": "aa", // 触发前缀
"body": [
"```python",
"$1", // 光标初始位置在中间行
"```"
],
"description": "Insert Python code block with backticks"
}
}
按照deepseek给出的说明完成操作,代码片段完全没有生效!
不生效的处理1
Deepseek进一步提示需要在设置中添加
"editor.suggest.showSnippets": true,
"editor.snippetSuggestions": "top", // 优先显示代码片段
设置中添加后,quarto文件生效,markdown文件仍然不生效。
不生效的处理2
网络搜索,给出的建议是设置文件中还需要添加:
"[markdown]": {
"editor.quickSuggestions": true
}
继续在设置中添加上面,Markdown文件中输入aa,显示提示,enter后生成片段头。
此时的已经添加的设置如下
"editor.suggest.showSnippets": true,
"editor.snippetSuggestions": "top", // 优先显示代码片段
"[markdown]": {
"editor.quickSuggestions": true
}
不生效的情况3
不生效很多时候是自定义的前缀无效,即没有出现提示,换个前缀。比如我这里"``"就不生效,换成"aa"后生效。
如果还是不生效,可以研究下面的配置:
"[markdown]": {
"editor.formatOnSave": false,
"editor.renderWhitespace": "all",
"editor.quickSuggestions": {
"other": true,
"comments": true,
"strings": true
},
"editor.acceptSuggestionOnEnter": "on"
}