44

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.

Yunus King
  • 1,141
  • 1
  • 11
  • 23

4 Answers4

28

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.

Chris Bain
  • 746
  • 8
  • 12
  • This saved my entire day! Works flawless. – 42tg Mar 15 '23 at 14:26
  • Excellent! I'm lovin' it! – Gabriel Jun 13 '23 at 05:57
  • is it possible to send different `args` for different workspace? For example I would like to run `npm start` in one workspace and `npm run dev` in another, using the same shortcut – Gangula Jun 14 '23 at 04:54
  • @Gangula An easy way to accomplish what you are trying to do would be to setup separate entries in `keybindings.json` as described above, one for `npm start` and one for `npm run dev` with each using different settingName in the when clause. – Chris Bain Jun 17 '23 at 16:38
  • Thank you @ChrisBain. I had actually taken inspiration from your approach & [this answer](https://stackoverflow.com/a/57189220/6908282) to come up with a different one which works very well for me. I added it in the [following answer](https://stackoverflow.com/a/76497629/6908282). – Gangula Jun 17 '23 at 18:37
  • @Gangula I'm glad you found a solution for your use case – Chris Bain Jun 24 '23 at 15:30
23

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.

HaaLeo
  • 10,065
  • 3
  • 44
  • 55
  • 3
    there is another issue raising this question in the backlog: https://github.com/Microsoft/vscode/issues/23757 – Ben Creasy Jun 13 '18 at 06:12
11

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
8c6b5df0d16ade6c
  • 1,910
  • 15
  • 15
0

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.

Chris Bain
  • 746
  • 8
  • 12
Gangula
  • 5,193
  • 4
  • 30
  • 59
  • This is a clever solution for using the same keyboard shortcut to run different VS Code tasks is different workspaces. Note that the first step (creating a task in the User `tasks.json` is not required. This task works as a default for the keybinding (if a task with that same label is not defined in the workspace), but the the solution would still work without it. – Chris Bain Jun 24 '23 at 15:31
  • I'm not sure I got your comment. if the `tasks.json` is not defined, then the keybinding would not be able to run any task right? – Gangula Jun 24 '23 at 17:49
  • In addition to defining tasks for a workspace in `.vscode/tasks.json`, you can also define [User Level Tasks](https://code.visualstudio.com/docs/editor/tasks#_user-level-tasks) via `tasks.json` in the VS Code User directory. Step 1 doesn't clearly state the task being created is a workspace-level task, and I mistakenly read it as a user-level task. Now that I understand that step 1 is to create a workspace-level task, you are correct and can ignore my earlier comment :-) – Chris Bain Jun 25 '23 at 18:29
  • Oh, I wasn't aware of User Level Tasks. Thanks for the reference and the clarification – Gangula Jun 25 '23 at 19:43