Yunus King
Yunus King

Reputation: 1271

A keybindings.json per workspace in Visual Studio Code

Is it possible to have a keybindings.json as part of the workspace settings instead of the user settings?

Since I have specific tasks associated to my workspace, I would also like to be able to assign workspace-specific shortcuts. Because I would use these shortcuts on my separate Windows and Linux environments, I would not prefer to see these key binding definitions end up amongst my environment-specific user settings.

Upvotes: 54

Views: 22649

Answers (4)

Gangula
Gangula

Reputation: 7332

I have implemented this by making using of tasks.json and a custom global keyboard shortcut. I have also written an article detailing out the entire process to set it up.

Below is the summary:

  1. create a task in your workspace .vscode/tasks.json called startDev - for example to run npm run dev command, like below:
       {
         "label": "startDev",
         "type": "npm",
         "script": "start:host" // you can even use shell scripts
       }
    
  2. add a custom shortcut in your global keybindings.json file like below:
    {
        "key": "ctrl+shift+/",
        "command": "workbench.action.tasks.runTask",
        "args": "startDev",
    },
    
  3. Now, you can add a task in all your projects' .vscode/tasks.json with same label and replace it with your desired task.

  • This approach is re-usable and extensible. You will need to set this up once and you'll be able to use it in any number of projects/workspaces
  • Another benefit of using tasks.json is, it has flags to switch commands based on the OS and also can use various terminals.

This article details the process and benefits of this approach in detail.

Upvotes: 1

8c6b5df0d16ade6c
8c6b5df0d16ade6c

Reputation: 2584

as of July 10, 2022, it doesn't seem possible to have a .vscode/keybindings.json per workspace, however it is possible to get keybindings that are active only when you're in a certain folder:

In your keybindings file, you can put:

[
  { 
    "key": "...",
    "command": "..." ,
    "when": "resourceDirname == '/Users/johndoe/path/to/folder'",
    ...
  },
]

You also have all these other contexts:

resource: "vscode-userdata:/Users/johndoe/path/to/file.json"
resourceDirname: "/Users/johndoe/path/to/"
resourceExtname: ".json"
resourceFilename: "file.json"
resourceLangId: "jsonc"
resourcePath: "/Users/johndoe/path/to/file.json"
resourceScheme: "vscode-userdata"

and more...


To find all possible context keys:

  1. Run the Developer: Toggle Developer Tools vscode command
  2. Then run the Developer: Inspect Context Keys vscode command and then click anywhere
  3. Expand the context object in the console, and you should see a full list of context key-value pairs

Upvotes: 20

Chris Bain
Chris Bain

Reputation: 965

Not a perfect solution, but I found a workaround to create a keybinding that can be activated at a workspace level.

In the workspace .vscode/settings.json file, set a new setting to true. (Because the setting is not registered by vscode or any extension, it will be displayed faded out in the editor - This is OK):

  "workspaceKeybindings.myAwesomeTask.enabled": true // setting can be anything you want

Then create the keyboard shortcut in the user keybindings.json, and add a "when" condition for the setting you created:

{ 
  "key": "ctrl+; ctrl+n",
  "command": "workbench.action.tasks.runTask",
  "args": "My Awesome Task",
  "when": "config.workspaceKeybindings.myAwesomeTask.enabled" // Must be "config.{settingName}"
},

This keybinding can be set as active in any workspaces you want.

Upvotes: 47

HaaLeo
HaaLeo

Reputation: 11762

This is not possible. Here Alexandru Dima (alexandrudima) explains why they do not want to add this feature and furthermore suggests to create your custom extension that contributes only keybindings to share them.

Upvotes: 26

Related Questions