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.

2 Answers 2

44

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"
    }
}
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?
    – Sam Joseph
    Dec 31, 2019 at 7:29
  • I've asked about this in the vscode github github.com/microsoft/vscode/issues/87965
    – Sam Joseph
    Jan 2, 2020 at 5:27
21

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"
    }
]
1
  • 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).
    – d3r3kk
    Feb 10, 2017 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.