在 vscode 中,如何定义快捷方式以调用当前活动语言的自定义代码段?
In vscode, how to define a shortcut to invoke a custom snippet for current active language?
假设我有一个 Ruby
用户代码段来输出字符串 ruby 和一个 Python
用户代码段来输出字符串 python。如何使 Ctrl+b 在 ruby 模式下调用上述 Ruby
片段,并调用Python
在 python 模式下的片段?
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "ruby"
},
"when": "editorLangId == ruby"
},
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "python"
},
"when": "editorLangId == python"
},
此外,您可以使用 langId
和 name
参数引用现有代码段,而不是使用代码段参数值来定义内联代码段。
https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts
https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers
https://code.visualstudio.com/docs/editor/userdefinedsnippets#_assign-keybindings-to-snippets
假设我有一个 Ruby
用户代码段来输出字符串 ruby 和一个 Python
用户代码段来输出字符串 python。如何使 Ctrl+b 在 ruby 模式下调用上述 Ruby
片段,并调用Python
在 python 模式下的片段?
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "ruby"
},
"when": "editorLangId == ruby"
},
{
"key": "ctrl+b",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "python"
},
"when": "editorLangId == python"
},
此外,您可以使用 langId
和 name
参数引用现有代码段,而不是使用代码段参数值来定义内联代码段。
https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts
https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers
https://code.visualstudio.com/docs/editor/userdefinedsnippets#_assign-keybindings-to-snippets