如何在VSCode中为扩展设置按键绑定?

17
我正在使用VSCode编写Swagger(OpenAPI)规范,并想利用特定的扩展来辅助编写该规范。
我安装的扩展没有为我提供一个键绑定,以便我可以轻松地调用它。
我该如何添加键绑定?我已尝试通过单击“文件”->“首选项”->“键盘快捷方式”并编辑keybindings.json文件来使其起作用,但到目前为止没有成功。
似乎我需要找到扩展的命令,但我不知道在哪里找到它,当我单击扩展中心,然后点击我想要使用的扩展时,在扩展摘要页面上也不容易看到它。
2个回答

44

如果有人正在为 VSCode 编写自己的扩展,可以使用 keybindings 属性和 contributes 属性中的 commands 设置默认的按键绑定。以下是由 Yeoman 的 yo code 命令初始化的示例项目的 package.json 中的设置示例:

{
    "name": "static-site-hero",
    "displayName": "Static site hero",
    "description": "Helps with writing posts for static site generator",
    "version": "0.0.1",
    "engines": {
        "vscode": "^1.30.0"
    },
    "categories": [
        "Other"
    ],
    "activationEvents": [
        "onCommand:extension.helloWorld",
        "onCommand:extension.insertLink",
        "onCommand:extension.insertFigure"
    ],
    "main": "./extension.js",
    "contributes": {
        "commands": [
            {
                "command": "extension.helloWorld",
                "title": "Hello World"
            },
            {
                "command": "extension.insertLink",
                "title": "Insert Markdown Link to File or Image"
            },
            {
                "command": "extension.insertFigure",
                "title": "Insert HTML figure"
            }
        ],
        "keybindings": [
            {
                "command": "extension.insertLink",
                "key": "ctrl+alt+l",
                "mac": "shift+cmd+f"
            },
            {
                "command": "extension.insertFigure",
                "key": "ctrl+alt+F",
                "mac": "shift+cmd+l"
            }
        ]
    },
    "scripts": {
        "postinstall": "node ./node_modules/vscode/bin/install",
        "test": "node ./node_modules/vscode/bin/test"
    },
    "devDependencies": {
        "typescript": "^3.1.4",
        "vscode": "^1.1.25",
        "eslint": "^4.11.0",
        "@types/node": "^8.10.25",
        "@types/mocha": "^2.2.42"
    }
}

我已经添加了这个,但是似乎仍然需要按照其他答案手动设置键绑定才能在开发模式下显示我的键绑定。是否有办法在开发模式下从package.json中提取键绑定? - Sam Joseph
我已在VS Code的Github上询问了此事 https://github.com/microsoft/vscode/issues/87965 - Sam Joseph

21
如果您打开扩展程序的信息窗口,您可能会看到一个“贡献”选项卡,在那里您可能会看到一个“命令”列表。

enter image description here

从那里你可以找到你想要的命令,并将其绑定到你的keybindings.json文件或文件->首选项->键盘快捷键中。

[
    {
        "key": "ctrl+enter",
        "command": "command.execute",
        "when": "editorTextFocus"
    }
]

1
啊耶!我之前看的时候没有发现。比我下面的发现要好得多。谢谢@Get Off My Lawn(顺便说一句,超棒的用户名)。 - d3r3kk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接