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

时间:2017-02-10 17:00:00

标签: visual-studio-code key-bindings vscode-settings

我正在使用VSCode编写Swagger(OpenAPI)规范,我想利用特定的扩展来帮助编写该规范。

我安装的扩展程序没有为我提供密钥绑定,可以轻松地使用它来调用它。

如何添加密钥绑定?我试图通过单击File-> Preferences-> Keyboard Shortcuts并编辑keybindings.json文件来使其工作,但到目前为止没有成功。

似乎我必须发现扩展程序的命令,我不知道在哪里找到它,在我点击扩展集线器上然后在扩展程序I上的扩展摘要页面上似乎不是很明显想用。

2 个答案:

答案 0 :(得分:6)

如果您打开扩展程序的信息窗口,则可能会看到Contributions标签,在那里您可能会看到Commands列表。

enter image description here

从那里,您可以在keybindings.json文件或File -> Preferences -> Keyboard Shortcuts

中找到所需的命令并将其绑定到该命令
[
    {
        "key": "ctrl+enter",
        "command": "command.execute",
        "when": "editorTextFocus"
    }
]

答案 1 :(得分:4)

如果有人为VSCode编写了自己的扩展名,则可以使用keybindings属性以及内部的props属性为命令设置默认的键绑定。由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",
            "when": "editorTextFocus"
        },
        {
            "command": "extension.insertFigure",
            "key": "ctrl+alt+F",
            "mac": "shift+cmd+l",
            "when": "editorTextFocus"
        }
    ]
},
"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"
}

}