首页 > 解决方案 > 在 vscode 中,如何定义一个快捷方式来调用当前活动语言的自定义代码段?

问题描述

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

标签: visual-studio-codekeyboard-shortcutscode-snippets

解决方案


{
    "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此外,您可以使用and参数引用现有的代码段,而不是使用代码段参数值来定义您的代码段内联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


推荐阅读