Trying to dynamically create keybindings from within an extension.
I can't seem to find any API that supports this.
Anyone knows?
This link should show you how to do it. https://code.visualstudio.com/api/references/contribution-points#contributes.keybindings
Steps:
register a new command (in extenson.ts)
let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
console.log("command ran");
});
context.subscriptions.push(disposable);`
Then add it to the activation events (in package.json)
"activationEvents": [
"onCommand:extension.helloWorld",
"onCommand:extension.newComment"
],
Then declare your new command (in package.json)
"contributes": {
"commands": [{
"command": "extension.helloWorld",
"title": "Hello World"
}],
"keybindings": [{
"command": "extension.helloWorld",
"key": "ctrl+k ctrl+k",
"mac": "cmd+k cmd+k",
"when": "editorTextFocus"
}]
},
Now whenever someone double presses k while holding cmd, the helloWorld command will run and "command ran" will be print to the console.
You can use Vscode namespace API for your extension. There is a keybindings section in your package.json. So you can define key map inside that.
For example you should use like this:
{ "key": "tab", "command": "tab", "when": ... },
{ "key": "tab", "command": "editor.emmet.action.expandAbbreviation", "when": ... },
{ "key": "tab", "command": "jumpToNextSnippetPlaceholder", "when": ... },
{ "key": "tab", "command": "acceptSelectedSuggestion", "when": ... },
{ "key": "ctrl+shift+k", "command": "editor.action.deleteLines", "when": "editorTextFocus" },
Extra helpful link: Windows virtual key list:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731
For detailed information about API and practices, please check following link:
https://code.visualstudio.com/docs/getstarted/keybindings#_customizing-shortcuts
You can do that by contributing all of your keybindings and using a custom where clause. See https://code.visualstudio.com/api/references/when-clause-contexts#add-a-custom-when-clause-context
Related
I'm looking for a shortcut that would clear the debug console + the terminal, and that would work when my cursor is on the editor.
I tried this code in the keybindings.json which only works for the terminal, and when the cursor is on the terminal (unless I removed the "when" part). But in any case this doesn't clear the debug console.
{
"key": "ctrl+k",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus"
},
{
"key": "ctrl+k",
"command": "workbench.debug.panel.action.clearReplAction",
"when": "inDebugRepl"
},
You will probably have to use a macro extension like multi-command that will allow you to run multiple commands.
In your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.clearTerminalandDebugConsole",
"sequence": [
"workbench.action.terminal.clear",
"workbench.debug.panel.action.clearReplAction"
]
}
]
and in keybindings.json:
{
"key": "ctrl+alt+k",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.clearTerminalandDebugConsole" },
// below since you wanted it to work with editor focus
"when": "editorTextFocus"
},
You used Ctrl-K but that is a sequence used in many already-bound conflicting commands, so I used Ctrl-Alt-K.
I have the following keybinding in VS Code which toggles the position of the cursor between the active document and built-in terminal:
// Toggle between terminal and editor focus
{
"key": "oem_8",
"command": "workbench.action.terminal.focus"
},
{
"key": "oem_8",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
}
Before i click the shortcut key to move the cursor to the terminal, i first have to save the active file.
I would therefore like to run the file saving command, which after searching on google i believe is workbench.action.files.save
How would i do this? I have tried adding the above code snippet at the end of the "command" line but it has not worked.
You would need a macro extension to run multiple commands from one keybinding.
I now use multi-command and there are other macro extensions now.
You can use this keybinding (in your keybindings.json) with the multi-command extension - no need for anything in settings.json:
{
"key": "oem_8", // or whatever keybinding you wish
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
]
},
"when": "editorTextFocus" // if you want this, you probably do
}
If you have more complicated macros you can still build them in your settings.json if you wish.
There is a way to run a sequence of commands without any extensions. It is by using Tasks. I like this method, because it allows to define a command, that is in workspace scope, not in global scope. The idea was taken from this blog post.
Example of tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmd-1",
"command": "${command:workbench.action.files.save}"
},
{
"label": "cmd-2",
"command": "${command:workbench.action.terminal.focus}"
},
{
"label": "cmd-All",
"dependsOrder": "sequence",
"dependsOn": [
"cmd-1",
"cmd-2"
],
}
]
}
Then in keybindings.json you just bind your hotkey to the task:
{
"key": "oem_8",
"command": "workbench.action.tasks.runTask",
"args": "cmd-All"
}
Note, that when defining a task, you can pass arguments to the command with the help of input variables.
Another extension to run multiple commands: Commands
{
"key": "oem_8",
"command": "commands.run",
"args": [
"workbench.action.files.save",
"workbench.action.terminal.focus"
],
"when": "editorTextFocus"
}
I made this extension. It's great.
I'm trying to make the key Ctrl+UpArrow execute both commands
cursorUp and
scrollLineUp.
I was hoping that this would work, but it doesn't:
{
"key": "ctrl+up",
"command": ["cursorUp", "scrollLineUp"], // This doesn't work
"when": "editorTextFocus"
}
How do I do that in VSCode?
This is currently not possible, but the corresponding feature request is tracked here. However you should take a look to the macros extension. It enables you to chain different commands to a single custom command. This custom command then can be bound to a hotkey.
In your case you could add this to your settings.json:
"macros": {
"myCustomCommand": [
"cursorUp",
"scrollLineUp"
]
}
And then add your custom hotkey to the keybindings.json:
{
"key": "ctrl+up",
"command": "macros.myCustomCommand"
}
There's a new way to achieve this without an extension:
Run "Tasks: Open User Tasks" command to create or open a user level tasks file.
Define commands as separate tasks, like so:
{
"version": "2.0.0",
"tasks": [
{
"label": "ctrlUp1",
"command": "${command:cursorUp}"
},
{
"label": "ctrlUp2",
"command": "${command:scrollLineUp}"
},
{
"label": "ctrlUpAll",
"dependsOrder": "sequence",
"dependsOn": [
"ctrlUp1",
"ctrlUp2"
],
"problemMatcher": []
}
]
}
In your keybindings.json:
{
"key": "ctrl+up",
"command": "workbench.action.tasks.runTask",
"args": "ctrlUpAll",
"when": "editorTextFocus"
}
("ctrlUpNNN" label format chosen for readability, task labels can be anything).
Any suggestions how to toggle between code and integrated terminal in VS Code?
In PowerShell ISE for example it's : Ctr+D terminal and Ctr+I code
Can't find anything similar for VS Code.
Thank you in advance for any suggestions
At current, the last post by sqlaide on this thread had a great answer (that works). You open up your keybindings.json* file and add the text below between the square brackets. Once complete, you can use Ctrl+` to move focus back and forth between the code and the terminal.
*File > Preferences > Keyboard Shortcuts and click on keybindings.json.
{
"key": "ctrl+`", "command": "workbench.action.terminal.focus",
"when": "!terminalFocus"},
{
"key": "ctrl+`", "command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"}
elaborating on the previous answer, I would like to share my working configuration to switch between Code and Terminal with or without full-sized Terminal.
NOTE: I tested this on my Mac, running VSCode on an EC2 instance.
settings.json
{
"multiCommand.commands": [
{
"command": "multiCommand.move2Terminal",
"sequence": [
"workbench.action.toggleMaximizedPanel",
"workbench.action.terminal.focus"
]
},
{
"command": "multiCommand.move2Code",
"sequence": [
"workbench.action.toggleMaximizedPanel",
"workbench.action.focusActiveEditorGroup"
]
}
]
}
keybindings.json
[
// Switch between Terminal and Code
{
"key": "shift+cmd+,",
"command": "workbench.action.terminal.focus",
"when": "!terminalFocus"
},
{
"key": "shift+cmd+,",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
}
// Switch to Terminal full-screen and back to Code
{
"key": "shift+cmd+.",
"command": "extension.multiCommand.execute",
"args": {
"command": "multiCommand.move2Terminal"
},
"when": "!terminalFocus"
},
{
"key": "shift+cmd+.",
"command": "extension.multiCommand.execute",
"args": {
"command": "multiCommand.move2Code"
},
"when": "terminalFocus"
},
]
Introduction
I am creating an extension in Visual Studio Code that creates a 'quickPick' menu from which the user can select options:
I can use the up and down arrows to scroll through the list, but I want to be able to bind this to something more home-row friendly like ctrl-n and ctrl-p. I have ctrl-n and ctrl-p already bound for scroll up/down on the main command menu (ctrl-shift-p), and I was hoping the quick pick would fall under this rule as well. Unfortunatley, none of my many ctrl-n context bindings are taking effect.
I'm hoping for something I can add to 'keybindings.json' that looks something like:
{
"key": "ctrl+n",
"command": "cursorDown",
"when": "quickPickFocus"
},
But I can't see anything like this when browsing through the "Default Keyboard Shortcuts".
Questions
How do you create key bindings for quick pick lists?
Can I possibly create a custom "when" context for my extension? Then I can specify something like:
"when" : "myExtensionIsActive && blah"
Additional Doc
Here are all the overridden ctrl-n key bindings in my keybindings.json:
{
"key": "ctrl+n",
"command": "cursorDown",
"when": "editorTextFocus"
},
{
"key": "ctrl+n",
"command": "workbench.action.quickOpenNavigateNext",
"when": "inQuickOpen"
},
{
"key": "ctrl+n",
"command": "showNextParameterHint",
"when": "editorTextFocus && parameterHintsVisible"
},
{
"key": "ctrl+n",
"command": "selectNextQuickFix",
"when": "editorFocus && quickFixWidgetVisible"
},
{
"key": "ctrl+n",
"command": "selectNextSuggestion",
"when": "editorTextFocus && suggestWidgetVisible"
},
Here is the code where I create the quickPick:
var themeList = this.getThemeList()
vscode.window.showQuickPick(themeList)
.then(val => {
// Update the status bar
this.cmdChannel.text = `Theme: ${val}`
this.cmdChannel.show();
});
You just add wrong key binding command and when, please try to add this to your keybindings.json
{
"key": "ctrl+n",
"command": "workbench.action.quickOpenSelectNext",
"when": "!editorFocus"
},
{
"key": "ctrl+p",
"command": "workbench.action.quickOpenSelectPrevious",
"when": "!editorFocus"
}