In VS Code, when the source control view is focused, I want to focus the first file in "staged Changes", navigate and focus between them, is there a keyboard shortcut to do this?
I want the behavior works like "Search: Focus Next Search Result,Search: Focus Previous Search Result". but in source view stage section
Here's a screenshot of what I'm talking about:
You can do this directly from anywhere in the scm view, but you will need a macro extension like Multi-Command. And this keybinding:
{
"key": "alt+u", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"list.focusFirst", // focuses the commit message input, as close as you can get
"list.focusDown",
"list.focusDown",
"list.focusDown",
"list.select" // add this to also open the scm diff
],
},
"when": "focusedView == workbench.scm"
}
When you are focused in the scm view you could go down/up a file and focus it with this keybinding:
{
"key": "alt+down", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"list.focusDown", // or "list.focusUp"
"list.select"
],
},
"when": "focusedView == workbench.scm"
}
To open the next item in the scm view and return focus to that scm view, try this keybinding:
{
"key": "alt+down", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"interval": 400, // need a delay
"sequence": [
"list.focusDown",
"list.select",
"workbench.action.focusSideBar",
],
},
"when": "focusedView == workbench.scm"
}
This is pretty easy and fast with regular keyboard navigation.
You can switch to the source control pane with the View: Show Source Control command, or the corresponding ctrl+shift+g shortcut. Or if it's already the current tab of the sidebar and the sidebar is not focused, focus it with ctrl+0.
Once you have focused on the source control view, just press up or down until you reach it.
If the listing is not focused yet, and something else like the commit message input or commit button is focused instead, just press tab or shift+tab until it is.
Related
I already found that the "command": "editor.action.duplicateSelection"
will duplicate the selection right next to it.
I want to duplicate the selected text to a new line. The selection may not be the entire line.
If you are talking about a selection that is less than the entire line, there is no built-in way to duplicate selected text to the next line. It can be done with a macro extension which enables you to run multiple commands at once.
Using the macro extension multi-command try this keybinding (in your keybindings.json):
{
"key": "alt+i", // whatever keybinding you want
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineAfter",
"editor.action.clipboardPasteAction",
{ // to add text after the selection
"command": "type", // you could also put this before the paste command
"args": { "text": " myText here after paste " }
}
]
}
}
That will copy the selected text, insert a blank line after it and paste that text there. Demo:
Demo with adding static text to the duplicated text:
Click File > Preferences > Keyboard Shortcuts:
Look for the Copy Line Down keyboard shortcut.
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.
I know there is a shortcut for comment and uncomment code block (SHIFT + ALT + A), but is there a way to quickly select (or even remove without select) block comment without using mouse or keyboard to select and press the delete/backspace button? For example:
/*
This is a large block of code with at least 50 lines of code!
:
:
*/
Is there a keyboard shortcut where I can place my cursor anywhere in the block comment and remove it in just a few keystrokes? Thanks!
You can set a macro to do this pretty easily.
First, use the excellent Select By extension (#rioV8) to select the text between and including the block comment markers /* and */. Put his into your settings:
"selectby.regexes": {
"BlockCommentSelect": {
"backward": "\/\\*",
"forward": "\\*\/",
"forwardInclude": true,
"backwardInclude": true,
"showSelection": true
}
},
You can use that with a keybinding like:
{
"key": "alt+s", // whatever keybinding you wish
"command": "selectby.regex",
"args": ["BlockCommentSelect"],
"when": "editorTextFocus"
},
You could stop here and use your keybinding to select the text and then Shift+Alt+A to toggle off the block comment.
Or you could add the selectby.regex1 to a macro and do it the selection and toggling off in one step. Here using the macro extension multi-command put this into your settings as well as the above selectby.regexes setting:
"multiCommand.commands": [
{
"command": "multiCommand.BlockCommentOff",
"sequence": [
{
"command": "selectby.regex",
"args": ["BlockCommentSelect"]
},
"editor.action.blockComment"
]
},
]
and then a keybinding to trigger that macro (in your keybindings.json):
{
"key": "shift+alt+A", // trigger the macro with whatever keybinding if you wish
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.BlockCommentOff" },
"when": "editorTextFocus && !editorHasSelection"
},
Here I used Shift+Alt+A to trigger the macro. And I used the when clause !editorHasSelection because if you have a selection maybe you want to block comment only that selection (inside another block comment!!).
Demos: (1) Just the first method where selectby selects your text and you manually toggle it off, and then (2) using the macro version to do it in one step.
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.