How can I change snippet trigger key to Ctrl+k? I tried these settings (from default keybindings file):
{
"key": "ctrl+k",
"command": "insertSnippet",
"when": "config.editor.tabCompletion && editorTextFocus && hasSnippetCompletions && !editorTabMovesFocus && !inSnippetMode"
}
but it doesn't work as expected, i.e. nothing happens.
I want to have this workflow:
type log in javascript file and hit Ctrl+k.
vscode looks for a log snippet and if it exist then expand snippet.
type something at current tabstop, hit Ctrl+k to go to another tabstop point.
For the last step there are jumpToNextSnippetPlaceholder command. But I'm stuck with initial action on first step. Can anyone help?
Cheers!
So, I investigated some time in reading documentation and ended up with following preferences (keybindings.json):
{
"key": "ctrl+k",
"command": "insertSnippet"
},
{
"key": "ctrl+k",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && editorHasSelection"
},
{
"key": "ctrl+k",
"command": "jumpToNextSnippetPlaceholder",
"when": "editorTextFocus && inSnippetMode"
},
{
"key": "shift+ctrl+k",
"command": "jumpToPrevSnippetPlaceholder",
"when": "editorTextFocus && inSnippetMode"
}
Maybe it could help anyone.
Related
I am working on OS Ubuntu 22.04 running inside a Virtualbox VM hosted in a windows 10 OS.
Inside the VM, it seems that my VScode has reset some of the user-defined keyboard shortcuts I have previously set.
So I want to re-define them.
I want to change the keyboard shortcut of "Toggle Line Comment", which is set from CTRL + Y to CTRL + ù ( currently CTRL + Y is assigned by the system to "redo", and that is OK ).
So I click on the pencil icon of "Toggle Line Comment",
press the keys combination CTRL + ù
"ù" gets interpreted as "[Backslash]"
press enter
but then I still see assigned CTRL + Y (as if the change was rejected); and from some tests I did, that one is the only combination that manages to toggle comment lines.
I have tryed to restart VScode but nothing changes, I cannot edit the settings from the UI.
So I have tryed to edit them from the keybindings.json
tommaso#tommaso-VirtualBox02:~$ sudo locate keybindings.json
/home/tommaso/.config/Code/User/keybindings.json
tommaso#tommaso-VirtualBox02:~$ vim /home/tommaso/.config/Code/User/keybindings.json
And the content of the opened file is
[
{
"key": "ctrl+alt+[Backslash]",
"command": "editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+shift+a",
"command": "-editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+shift+7",
"command": "-editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+[Backslash]",
"command": "editor.action.commentLine"
}
]
It is indeed strange that the last {} entry I have added via the UI is missing the "when" key.
Anyway I have edited the content to
[
{
"key": "ctrl+alt+[Backslash]",
"command": "editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+shift+a",
"command": "-editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+shift+7",
"command": "-editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+[Backslash]",
"command": "editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
}
]
saved,
restarted VScode
but again, the "toggle line comment" gets activated only by CTRL + Y.
The strange thing is that the CTRL + ALT + ù, that is
{
"key": "ctrl+alt+[Backslash]",
"command": "editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
}
works fine.
It is like VS code cannot load changes from keybindings.json .
What can be blocking the edit?
I believe you would do this like so:
[
{
"key": "ctrl+\\",
"command": "editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+y",
"command": "-editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
]
From another machine having the same key VScode shortcut configuration, and having the shotcuts properly working on VScode, I accessed the content of keybinding.json, and it is the following
// Place your key bindings in this file to override the defaultsauto[]
[
{
"key": "ctrl+shift+[Backslash]",
"command": "editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+shift+a",
"command": "-editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+[Backslash]",
"command": "editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+shift+7",
"command": "-editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
}
]
It is exactly the same content of the one on the other machine, for which it was not working properly.
So the problem is machine-related.
So the end I have set the key combination ctrl+à for the toggle line comment, and it is OK.
I think the problem is somehow linked to how VScode interprets a key:
I have an italian keyboard having a key which contains "§" and "ù", but VS code somehow interprets it as "[Backslash]".
Once again I want to underline that it is strange to me that
ctrl+alt+ù
is correctly got as change by VScode, while
ctrl+ù
is rejected ( the UI swithces the combination automatically to "ctrl"+"y", while on the keybindings.json it is actually written "ctrl+[Backslash]" )
even if in both cases 1) and 2), "ù" is interpreted as "[Backslash]".
VScodevim has extension.vim_ctrl+j by default mapped to Ctrl+j which allows you do navigate down pop-up code suggestion windows (triggered by hitting Ctrl+Space in insert mode) like this:
It also has extension.vim_ctrl+k mapped to Ctrl+k but this binding down not work, so I cannot scroll up pop-up code suggestion windows. in insert mode defaults to entering a digraph but simply adding something like this
{
"before": ["<C-k>"],
"after": ["extension.vim_ctrl+k"]
}
to my settings.json does not work since although it removes the digraph functionality, from what I understand, whenever I now press Ctrl+k in insert mode, VSCode will consult the settings.json, find the mapping of to "extension.vim_ctrl+k" which points it back to settings.json in a sort of infinite loop.
:h i_ctrl-j in vim reveals this keybind to be mapped to "Begin new line" so it seems VScode interprets "Begin new line" as navigating down a pop-up window in insert mode instead of it's usual vim behaviour of creating a new line and moving the cursor there, although not sure this is how it works. In any case, I could not find an equivalent vim command that perhaps VSCode could use to scroll up in pop-up windows. Any help would be much appreciated!
I got this working by copying the existing default keybinds for ctrl+p and ctrl+n which are used throughout VSCode by default for scrolling up and down.
I've used alt here in my keybinds.json file but you can easily replace it with ctrl to achieve what you want
keybinds.json
// Down Motion
{
"key": "alt+j",
"command": "cursorDown",
"when": "textInputFocus"
},
{
"key": "alt+j",
"command": "showNextParameterHint",
"when": "editorFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
},
{
"key": "alt+j",
"command": "selectNextSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "alt+j",
"command": "list.focusDown",
"when": "listFocus && !inputFocus"
},
{
"key": "alt+j",
"command": "workbench.action.quickOpenSelectNext",
"when": "inQuickOpen"
},
// Up Motion
{
"key": "alt+k",
"command": "cursorUp",
"when": "textInputFocus"
},
{
"key": "alt+k",
"command": "showPrevParameterHint",
"when": "editorFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
},
{
"key": "alt+k",
"command": "selectPrevSuggestion",
"when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
},
{
"key": "alt+k",
"command": "list.focusUp",
"when": "listFocus && !inputFocus"
},
{
"key": "alt+k",
"command": "workbench.action.quickOpenSelectPrevious",
"when": "inQuickOpen"
}
I use keyboard shortcuts to cycle through tabs using workbench.action.nextEditorInGroup and workbench.action.previousEditorInGroup which works great for me except that they "wrap". In other words, if you are on the first tab of an editor group and you execute workbench.action.previousEditorInGroup it will focus the last editor in the group. There doesn't seem to be a setting for preventing this behavior (though if you know of one that would solve my problem!), so I'm hoping to achieve it through the when clause in the keybindings.
I found activeEditorGroupIndex and activeEditorGroupLast, but I have not found a similar clause for the editor index. Does anyone know of one?
Here's my current bindings:
{ "key": "ctrl+.", "command": "workbench.action.nextEditorInGroup", "when": "editorFocus" },
{ "key": "ctrl+,", "command": "workbench.action.previousEditorInGroup", "when": "editorFocus" },
What I'd like is something like:
{ "key": "ctrl+.", "command": "workbench.action.nextEditorInGroup", "when": "editorFocus && !activeEditorLast" },
{ "key": "ctrl+,", "command": "workbench.action.previousEditorInGroup", "when": "editorFocus && !activeEditorIndex == 0" },
These new context keys are in v1.66:
activeEditorIsFirstInGroup
activeEditorIsLastInGroup
New context keys were added to figure out if an editor is first or
last in an editor group:
activeEditorIsFirstInGroup- Whether the active editor is the first one in its group.
activeEditorIsLastInGroup- Whether the active editor is the last one in its group.
see https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_66.md#new-context-keys-for-editors
I would think these keybindings should work for you:
{ "key": "ctrl+.", "command": "workbench.action.nextEditorInGroup", "when": "editorFocus && !activeEditorIsLastInGroup" },
{ "key": "ctrl+,", "command": "workbench.action.previousEditorInGroup", "when": "editorFocus && !activeEditorIsFirstInGroup" }
This old Emacs user, who is used to elpy, is attempting to move onto VSCode with Scala & more specifically Ammonite repl.
I used Ctrl+' to open the integrated terminal & all I have to do is type amm on the bash shell (ubuntu) to open the repl; however, I still miss being able to send the either the line or selection from the editor to integrated shell with Ctrl+Enter.
I guess this means a bit of coding. Where can I start? Has anyone accomplished similar?
Thanks much,
If you already have your terminal and REPL open, there is a built in command called "Run Selected Text in Active Terminal" / workbench.action.terminal.runSelectedText.
It has no default keybinding, so you need to set it yourself. Something like this would work:
{
"key": "ctrl+enter",
"command": "workbench.action.terminal.runSelectedText",
"when": "editorTextFocus && editorHasSelection"
}
Actually, I found that adding VSCode Macros extension does the job:
I just changed settings.json:
{
"window.zoomLevel": 1,
"editor.fontSize": 11,
"terminal.integrated.fontSize": 11,
"macros": {
"execCurLn": [
"cursorUp",
"expandLineSelection",
"workbench.action.terminal.runSelectedText",
"cancelSelection"
]
}
}
and added (1st part is pure #kwood & thank u again) to keybindings.json
{
"key": "ctrl+enter",
"command": "workbench.action.terminal.runSelectedText",
"when": "editorTextFocus && editorHasSelection"
}
{
"key": "ctrl+enter",
"command": "macros.execCurLn",
"when": "editorTextFocus && !editorHasSelection"
},
{ "key": "ctrl+`", "command": "workbench.action.terminal.focus"},
{ "key": "ctrl+`", "command": "workbench.action.focusActiveEditorGroup", "when": "terminalFocus"}
Open the command palette with CTRL+SHIFT+P and look for Terminal: Run Selected Text In Active Terminal. On the left you will see the key binding or a wheel engine to set the binding.
follow another post VS Code move to next line on run ctrl + enter, to run current line then cursor down, avoiding running next line unexpectedly
in settings.json, add
"macros": {
"pythonExecSelectionAndCursorDown": [
"python.execSelectionInTerminal",
"cursorDown",
]
}
in keybindings.json, add
{
"key": "ctrl+enter",
"command": "macros.pythonExecSelectionAndCursorDown",
"when": "editorTextFocus && editorLangId == 'python'"
},
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"
}