How do I set a keybinding for an extension in VSCode?
Asked Answered
O

2

17

I am using VSCode to write a Swagger (OpenAPI) specification and I want to make use of a specific extension to aid in the writing of that specification.

The extension I have installed does not supply a key binding for me to easily invoke it with.

How do I go about adding the key binding? I have attempted to make it work by clicking File->Preferences->Keyboard Shortcuts and editing the keybindings.json file, but without success thus far.

It seems I have to discover the extension's command and I don't know where to find that, doesn't seem to be readily apparent on the extension summary page either when I click on the extensions hub, then on the extension I want to use.

Overtake answered 10/2, 2017 at 17:0 Comment(0)
J
22

If you open your extension's information window, you might see a Contributions tab, and in there you might see a Commands list.

enter image description here

From there you can find the command that you want and bind to it in your keybindings.json file or File -> Preferences -> Keyboard Shortcuts

[
    {
        "key": "ctrl+enter",
        "command": "command.execute",
        "when": "editorTextFocus"
    }
]
Judson answered 10/2, 2017 at 17:9 Comment(1)
Ah yeah! I didn't see that when I'd looked. Much better than my discovery below. Thanks @Get Off My Lawn (and awesome username BTW).Overtake
A
46

In case somebody is writing their own extension for VSCode, you can set up a default key binding for your commands using the keybindings prop alongside with commands inside contributes prop. Example setup in package.json of a sample project inited by Yeoman yo code command:

{
    "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"
    }
}
Aircrew answered 26/12, 2018 at 21:19 Comment(2)
I've added this, but I still seem to have to set up keybindings manually as per the other answer in order to get my keybindings to show up in development mode. Is there any way to have keybindings pulled in from package.json when running in development mode?Headstand
I've asked about this in the vscode github github.com/microsoft/vscode/issues/87965Headstand
J
22

If you open your extension's information window, you might see a Contributions tab, and in there you might see a Commands list.

enter image description here

From there you can find the command that you want and bind to it in your keybindings.json file or File -> Preferences -> Keyboard Shortcuts

[
    {
        "key": "ctrl+enter",
        "command": "command.execute",
        "when": "editorTextFocus"
    }
]
Judson answered 10/2, 2017 at 17:9 Comment(1)
Ah yeah! I didn't see that when I'd looked. Much better than my discovery below. Thanks @Get Off My Lawn (and awesome username BTW).Overtake

© 2022 - 2024 — McMap. All rights reserved.