Reputation: 1271
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
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:
.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
}
keybindings.json
file like below:
{
"key": "ctrl+shift+/",
"command": "workbench.action.tasks.runTask",
"args": "startDev",
},
.vscode/tasks.json
with same label and replace it with your desired task.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
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:
Developer: Toggle Developer Tools
vscode commandDeveloper: Inspect Context Keys
vscode command and then click anywhereUpvotes: 20
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