My issue:
I would like to bind a few keyboard shortcuts to navigate to specific symbols in my currently open file within VS Code.
Example:
I want to navigate to the symbol <template> by pressing cmd+1.
I want to navigate to the symbol <script> by pressing cmd+2.
I want to navigate to the symbol <style> by pressing cmd+3.
By default, you can do that by pressing cmd+p and then in the palette typing #template which will jump to the template tag within the open file.
So a solution might look something like this in my keybinding.json:
{
"key": "cmd+1",
"command": "workbench.action.gotoSymbol",
"args": ["#template"],
}
However, the args part of this does not work. I'm wondering if anyone knows a good solution for setting up key bindings like this so I can navigate around my files with shortcuts.
Thanks in advance!
Try this:
{
"key": "cmd+1",
"command": "workbench.action.quickOpen",
"args": "#<template>",
},
The command workbench.action.gotoSymbol won't take an argument, but workbench.action.quickOpen will. I don't know why there is that difference, but as you know if you start the Quick Open Panel with # you get object references. And the rest of your text <template> for example will automatically be entered into the input box.
It will filter for those symbols but will not jump to the first one. If you want that additional functionality, you would have to consider a macro which would select the first entry.
There is an alternative that allows you to navigate to anything by using a matching regex. See the extension Select By which allows to jump to any string. For example (in settings.json):
"selectby.regexes": {
"goTo<template>": {
"moveby": "<template>",
}
}
and your keybinding (keybindings.json):
{
"key": "ctrl+1", // <template>`
"command": "moveby.regex",
"args": ["go<template>", "moveby", "next", "start", "wrap"],
// "when": "editorTextFocus && editorLangId == javascriptreact"
}
You can set it up to jump to the previous instance as well:
{
"key": "ctrl+alt+1", // previous <template>
"command": "moveby.regex",
"args": ["goTo<template>", "moveby", "prev", "start", "wrap"],
// "when": "editorTextFocus && editorLangId == javascriptreact"
}
either way it should now "wrap", it just depends on how many <templates> you have in the file and which direction you want to go to first.
I found the solution to my problem, which is pretty funny because it's an existing extension for VS Code that does exactly what I am looking for:
https://marketplace.visualstudio.com/items?itemName=andersonmfjr.vue-jumptotag
To bind the keyboard shortcuts in keybindings.json
{
"key": "cmd+1",
"command": "extension.jumptotemplate",
},
{
"key": "cmd+2",
"command": "extension.jumptoscript",
},
{
"key": "cmd+3",
"command": "extension.jumptostyle",
},
That does exactly what I was looking for.
Related
I'd like to add in custom keyboard shortcuts - rather than just remapping existing key bindings. Is this possible?
The idea is to map a shortcut that will allow me to include an easily identifiable comment header to help index my projects - in this format:
/*--------------------------------------------------------------
# Header
--------------------------------------------------------------*/
Here is a snippet (which is triggered by typing cHeader):
"Custom Header": {
"prefix": ["cHeader"],
"body": [
"/*---------------------------------------------------------------",
"# $1",
"---------------------------------------------------------------*/"
]
}
You can make that to whatever length you want. For more complicated situations, see https://stackoverflow.com/a/56874352/836330 or https://stackoverflow.com/a/58722958/836330.
If you want to set a keybinding to that, use this:
{
"key": "ctrl+alt+r", // whatever you want as a keybinding
"command": "editor.action.insertSnippet",
"args": {
"name": "Custom Header" // name from your snippet above
},
"when": "editorTextFocus"
}
I want to define a single and universal keyboard binding that allows me to toggle the maximization of the current window, regardless of the type of window.
At a minimum, a window could be:
Any single editor group
The panel itself
By maximization, note that I mean in both height and width so that the window takes over the full application window with the exception of the side bar (i.e. if the side bar is already visible, it remains visible at all times).
By toggling I mean of course going back and forth between the original layout (i.e. remembering it), and the maximization result.
My thinking about this problem
There are several command IDs that I believe could help here but none of them seem to do what I want, e.g.
workbench.action.toggleEditorWidths
workbench.action.maximizeEditor
workbench.action.toggleMaximizePanel
workbench.action.toggleEditorVisibility
I suspect that
there are more commands that could help here
there is probably a way to accomplish this with the right boolean logic of when clauses and joint execution of command IDs (?)
Try this in your keybindings.json (chose your desired keybinding):
{
"key": "alt+m",
"command": "workbench.action.toggleMaximizedPanel",
"when": "terminalFocus"
},
{
"key": "alt+m",
"command": "workbench.action.closePanel",
"when": "editorFocus && panelIsOpen"
},
{
"key": "alt+m",
"command": "workbench.action.togglePanel",
"when": "editorFocus && !panelIsOpen"
},
It works well - has no effect on the SideBar or ActivityBar - except for one thing. The workbench.action.togglePanel command in the third keybinding will appear to toggle the maximized editor but that command shifts focus to the opened panel. Which might not be what your expect.
If you don't want that focus shift to the panel when un-maximizing the editor I think you will have to add a macro to the mix. Disable the second and third keybindings and add this to your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.toggleEditorWthFocus",
"sequence": [
"workbench.action.togglePanel",
"workbench.action.focusActiveEditorGroup",
"workbench.action.toggleEditorWidths" // will toggle other editor groups width
// but other editor group width goes to a built-in minimum, not 0
]
}
],
using the multi-command macro extension.
and use these as your two keybindings:
{
"key": "alt+m",
"command": "workbench.action.toggleMaximizedPanel",
"when": "terminalFocus"
},
//{
// "key": "alt+m",
// "command": "workbench.action.closePanel",
// "when": "editorFocus && panelIsOpen"
//},
// {
// "key": "alt+m",
// "command": "workbench.action.togglePanel",
// "when": "editorFocus && !panelIsOpen"
// },
{
"key": "alt+m",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.toggleEditorWthFocus" },
"when": "editorTextFocus && !panelIsOpen"
},
I know you said you want the SideBar unchanged, but if you go the macro route I think it would be pretty easy to toggle that as well.
The comments above note that if you have more than one editor group, unfocused editor groups' width will be minimized, but it goes to some built-in minimum and not to 0 width. I don't think there is a way to toggle other editor groups on/off other than by closing other editor groups which can be done but you probably don't want that.
Demo with split:
How do I search for the selected text in the current file without having to copy / ctrl-f / paste?
For clarification: Ultraedit has this behaviour. When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.
Enable the editor.find.seedSearchStringFromSelection setting as seen below. This will cause highlighted texted to automatically be searched when you press ctrl+f.
Ultraedit-way search is my favorite and it's really convenient: single 'F3' key can handle all.
The drawback of Ctrl+D is: it cannot wrap around the search.
To be clear, the definition of Ultraedit-way search is:
When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.
Here is the absolutely 100%-compatible solution to Ultraedit-way search:
When text is selected, do nextSelectionMatchFindAction
When no text is selected, do nextMatchFindAction
Single F3 does both above.
Shift+F3 keeps its original: Find Previous
Therefore keybindings.json can add the following lines to disable original F3 and Ctrl+F3 functions, and add two new F3 functions when text is selected and not selected.
{
"key": "f3",
"command": "editor.action.nextSelectionMatchFindAction",
"when": "editorFocus && editorHasSelection"
},
{
"key": "ctrl+f3",
"command": "-editor.action.nextSelectionMatchFindAction",
"when": "editorFocus"
},
{
"key": "f3",
"command": "editor.action.nextMatchFindAction",
"when": "editorFocus && !editorHasSelection"
},
{
"key": "f3",
"command": "-editor.action.nextMatchFindAction",
"when": "editorFocus"
}
And one more thing to be addressed:
When F3 is pressed, the search dialog is appeared and every matched text is highlighted, you can press ESC to dismiss search dialog when search is done.
Update #2021/1/25
If anyone wants Shift+F3 to works as smart as F3, add the following line into keybindings.json:
{
"key": "shift+f3",
"command": "editor.action.previousSelectionMatchFindAction",
"when": "editorFocus && editorHasSelection"
},
{
"key": "shift+f3",
"command": "editor.action.previousMatchFindAction",
"when": "editorFocus && !editorHasSelection"
},
{
"key": "shift+f3",
"command": "-editor.action.previousMatchFindAction",
"when": "editorFocus"
},
I have found what I was looking for.
Ctrl+D triggers the action Add Selection To Next Find Match which does exactly what I wanted: perform an immediate search for the selected text, just like F3 in Ultraedit.
Does anyone know how "Replace with next value" (ctr + shift + dot) works ?
I am unable to get this shortcut to do anything on my vscode.
I tried googling examples of this shortcut and asking on vscode gitter/reddit but couldn't get any answers.
It would be greatly appreciated if someone could provide the steps of using this shortcut.
If I press the shortcut after highlighting an integer or float literal (16 or 5.2 for example) it subtracts 1 from it.
Strangely, Replace with Previous Value adds 1 to the value while Replace with Next Value subtracts 1.
I don't know if it has any other purpose.
Add to the accepted answer:
If you use it on a boolean, it toggles between true and false.
No need to "highlight" the number, putting the cursor on it is enough.
As noted by #FĂ©lix Caron, this is how to swap the two keys.
keybindings.json
{ "key": "ctrl+shift+oem_period", "command": "editor.action.inPlaceReplace.up", "when": "editorTextFocus && !editorReadonly" },
{ "key": "ctrl+shift+oem_comma", "command": "editor.action.inPlaceReplace.down", "when": "editorTextFocus && !editorReadonly" },
Moreover, in my case as I have breadcrumbs enabled "breadcrumbs.enabled": true,, the replaceUp key triggered breadcrumbs instead, so I had to unbind it.
{ "key": "ctrl+shift+oem_period", "command": "-breadcrumbs.toggleToOn", "when": "!config.breadcrumbs.enabled" },
{ "key": "ctrl+shift+oem_period", "command": "-breadcrumbs.focusAndSelect", "when": "breadcrumbsPossible" },
I know you can enter a block of text with code snippets but can you configure keyboard shortcuts to enter some text?
With "editor.action" you can move the cursor but I can't find if it's possible if you can get it to type some text.
Something like Ctrl+Enter would be "); then a new line
Maybe create a code snippet and then invoke it with a keyboard shortcut?
Is there a way to find what all the options are for "editor.action" ?
You can insert a User Snippet on keypress:
Open keybindings.json (Preferences: Open Keyboard Shortcuts (JSON)), which defines all your keybindings, and add a keybinding passing "snippet" as an extra argument
{
"key": "ctrl+enter",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "\");\n$0"
}
}
Furthermore, you can specify languages in which it should work:
"when": "editorTextFocus && editorLangId == 'javascript'"
See here for more information.
You can also use the simple command type in a keybinding like:
{
"key": "ctrl+enter",
"command": "type",
"args": {
"text": "myText"
},
"when": "editorTextFocus"
},
The list of available keyboard actions is available from here. You can consider to write an extension for VS Code if you have something specific in mind, with that you can create actions with keybindings that modify the editor contents.
I just leave it here. An alias for triple backticks for people with non-English keyboards:
{
"key": "ctrl+shift+1",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus && !editorReadonly && editorLangId == 'markdown'",
"args":{
"snippet": "```"
}
}