A keybindings.json per workspace in Visual Studio Code
Asked Answered
S

4

51

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.

Silkweed answered 23/2, 2018 at 10:3 Comment(0)
A
25

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.

Aide answered 23/2, 2018 at 11:0 Comment(2)
there is another issue raising this question in the backlog: github.com/Microsoft/vscode/issues/23757Clank
I think the explanation on the GitHub issues is irrelevant to the question. He explains why he thinks it does not make sense to share key bindings. All good. But the question is not about sharing, the question is about ability to have same shortcut doing different things depending on the workspace. In the same vein "create your custom extension that contributes only keybindings to share" is this answer does nothing to address the original question since it did not ask about sharingKrystynakshatriya
G
41

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.

Gennie answered 18/10, 2021 at 3:56 Comment(9)
This saved my entire day! Works flawless.Visional
Excellent! I'm lovin' it!Anaplasty
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 shortcutBrocky
@Brocky 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.Gennie
Thank you @ChrisBain. I had actually taken inspiration from your approach & this answer to come up with a different one which works very well for me. I added it in the following answer.Brocky
@Brocky I'm glad you found a solution for your use caseGennie
There is perfect to me! Negtive config is like this: {"when": "!config.workspaceKeybindings.myAwesomeTask.enabled && (HERE IS OTHER CONDITIONS)"}Gwendolin
Is there a convention for these arbitrary settings, hopefully not to be faded by vscode?Firepower
@Firepower Nope, no convention. In my example, I used a name that would hopefully make sense when I see it 2 years later and can't remember what it is LOL. But whatever setting name you use, it will be an unknown setting to VS Code (because no extension defines it) and will be faded out in the JSON editor. I'm not aware of any way around that with this solution.Gennie
A
25

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.

Aide answered 23/2, 2018 at 11:0 Comment(2)
there is another issue raising this question in the backlog: github.com/Microsoft/vscode/issues/23757Clank
I think the explanation on the GitHub issues is irrelevant to the question. He explains why he thinks it does not make sense to share key bindings. All good. But the question is not about sharing, the question is about ability to have same shortcut doing different things depending on the workspace. In the same vein "create your custom extension that contributes only keybindings to share" is this answer does nothing to address the original question since it did not ask about sharingKrystynakshatriya
W
17

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
Worn answered 10/7, 2022 at 8:56 Comment(0)
B
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.

Brocky answered 17/6, 2023 at 18:35 Comment(4)
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.Gennie
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?Brocky
In addition to defining tasks for a workspace in .vscode/tasks.json, you can also define 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 :-)Gennie
Oh, I wasn't aware of User Level Tasks. Thanks for the reference and the clarificationBrocky

© 2022 - 2025 — McMap. All rights reserved.