Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 608
d3r3kk Avatar asked Feb 10 '17 17:02

d3r3kk


People also ask

How do I change the default key bindings in VS Code?

Click File -> Preferences -> Keyboard shortcuts. Use the tab that opens up to edit and find available key bindings and assign them.

How do I set Keybinds in Visual Studio?

On the menu bar, choose Tools > Options. Expand Environment, and then choose Keyboard. Optional: Filter the list of commands by entering all or part of the name of the command, without spaces, in the Show commands containing box. In the list, choose the command to which you want to assign a keyboard shortcut.

How do I configure extensions in VS Code?

You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.


1 Answers

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"
    }
}
like image 145
Tore Aurstad Avatar answered Sep 27 '22 19:09

Tore Aurstad