在vscode中,如何定义一种快捷方式来调用当前活动语言的自定义代码段?

时间:2018-12-04 19:12:36

标签: visual-studio-code keyboard-shortcuts code-snippets

假设我有一个Ruby用户代码段来输出字符串 ruby​​ 和一个Python用户代码段来输出字符串 python 。如何在ruby模式下使 Ctrl + b 来调用上述Ruby代码段,而在python-模式下如何调用Python代码段模式?

1 个答案:

答案 0 :(得分:1)

{
    "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"
},

此外,您可以使用langIdname参数来引用现有的代码段,而不是使用snippet参数值来定义代码段内联。

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