visual studio code go to a tab by number - visual-studio-code

In chrome and most other browsers/editors, we can go to a particular tab by pressing the Command key and the number. For example: If we press Command+1 we will go to the first tab, Command+2 takes to the second tab, etc.
Is it possible to get such a key mapping for Visual studio code ?

Since VSCode uses ctrl+number by default (which changes desktops in macOS), you can use cmd+number with custom keybindings:
Paste this in your keybindings.user:
{
"key": "cmd+1",
"command": "workbench.action.openEditorAtIndex1"
},
{
"key": "cmd+2",
"command": "workbench.action.openEditorAtIndex2"
},
{
"key": "cmd+3",
"command": "workbench.action.openEditorAtIndex3"
},
{
"key": "cmd+4",
"command": "workbench.action.openEditorAtIndex4"
},
{
"key": "cmd+5",
"command": "workbench.action.openEditorAtIndex5"
},
{
"key": "cmd+6",
"command": "workbench.action.openEditorAtIndex6"
},
{
"key": "cmd+7",
"command": "workbench.action.openEditorAtIndex7"
},
{
"key": "cmd+8",
"command": "workbench.action.openEditorAtIndex8"
},
{
"key": "cmd+9",
"command": "workbench.action.openEditorAtIndex9"
},

This is available now, with the latest version of Visual Studio Code. See: https://github.com/Microsoft/vscode/issues/24753#issuecomment-294518439

On macOS, the shortcuts default to ctrl + n, not cmd + n. You can fix that in Code -> Preferences -> Keyboard Shortcuts.
Another note: most other apps activate the last tab with cmd + 9. You get that behavior by using workbench.action.lastEditorInGroup instead of workbench.action.openEditorAtIndex9.

Unfortunately, no. But you can configure shortcuts to move between tabs, such as going to the previous, next, previously modified, etc. tabs. You can make changes in the Options menu under Keyboard.

This comes standard with the "Emacs Keymap" extension as well as a multitude of other "never touch the mouse" keybindings that I, personally, find keep my stress level way down while navigating code.
Now all I need to find is a way to put the tab's number on the tab itself (which is what I was searching when I found this question).

Related

is it possible to navigate using j, k command in vscode source action menu?

When a popup like a video below is opened through this source action in the vscode editor, how do I navigate the menu using the j and k keys?
image vscode editor
temporary solution
I ended up solving this problem with a better touch tool or a karabiner keymapping tool. If you set it like the image below, you can navigate with ctrl + j,k keys. Of course, there are also side effects. You cannot use ctrl + j,k with the vim command. But since I don't use that command, this was enough to solve my problem.
keymapping image
It looks like in v1.71 these command id's will be changing, see Align new code action widget command names with:
We should try to align these command names with that ones that already
exist for the suggestion widget
onEnterSelectCodeAction -> acceptSelectedCodeAction
(acceptSelectedSuggestion)
focusNextCodeAction -> selectNextCodeAction
(selectNextSuggestion)
focusPreviousCodeAction -> selectPrevCodeAction
(selectPrevSuggestion)
Coming to vscode v1.70 are some commands for navigating the code actions menu (or quickfix menu - previous answer covers both).
Sample keybindings:
{
"key": "ctrl+k",
"command": "focusNextCodeAction", // in v1.70
// "command": "selectNextCodeAction" // in v1.71
"when": "codeActionMenuVisible"
},
{
"key": "down",
"command": "-focusNextCodeAction", // in v1.70
// "command": "-selectNextCodeAction", // in v1.71
"when": "codeActionMenuVisible"
},
{
"key": "ctrl+j",
"command": "focusPreviousCodeAction", // in v1.70
// "command": "selectPrevCodeAction", // in v1.71
"when": "codeActionMenuVisible"
},
{
"key": "ctrl+up",
"command": "-focusPreviousCodeAction", // in v1.70
// "command": "selectPrevCodeAction", // in v1.71
"when": "codeActionMenuVisible"
},
There [wasn't, see above] a built-in way to do that, see the github issue Missing keybinding for navigation in Quick Fix contextual menu.
There are a couple of workarounds mentioned in that issue, including the extension Keyboard Quickfix, made specifically for this issue.

VSCode how to know when editor is not split?

Until now, I used cmd+2 to split the editor on Visual Studio Code and then cmd+1 and cmd+2 to move between the split editors. This shortcut stopped working for some reason, and I can't set it on the keyboard shortcut menu either.
I checked with another keyboard, so this is not an hardware issue.
Anyhow, I tried to set new keybindings to split the screen and move between the editors, like this:
[
{
"key": "cmd+shift+]",
"command": "workbench.action.navigateRight",
"when": ""
},
{
"key": "cmd+shift+]",
"command": "workbench.action.splitEditorRight",
"when": ""
},
{
"key": "cmd+shift+[",
"command": "workbench.action.navigateLeft",
},
]
The only problem, I couldn't find what I need to write in the when option.
I want my shortcut to split the editor only if the editor is not split yet, and to move between the editors when it is.
How can I achieve that?
The multipleEditorGroups when clause context returns true when there's more than one editor group open.
You don't need to put a "when" for neither of them. Just checked vscode defaults and they don't have any.

How to move up and down in quick fix suggestions in visual studio code without using arrow keys?

Whenever there is some missing or needs correction in code, visual studio code shows a yellow bulb to show some quick fixes suggestions.
Is there any way or shortcut by which we can navigate up and down suggestion options shown as in screenshot above without using the arrow keys?
In vscode v1.71 there are three new commands you can use for navigating in the QuickFix menu to go to previous or next source code actions.
selectNextCodeAction // to focus the nextcode action
selectPrevCodeAction // to focus the previous code action
acceptSelectedCodeAction // to run the focused/selected code action
You can see the default keybindings below that have been removed. Note the - before the command name in two of the keybindings, that removes those keybindings.
You can make these keybindings (in your keybindings.json):
{
"key": "alt+u", // whatever you want here
"command": "selectPrevCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "up", // default removed
"command": "-selectPrevCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "alt+d", // whatever you want here
"command": "selectNextCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
},
{
"key": "down", // default removed
"command": "-selectNextCodeAction", // for v1.71
"when": "CodeActionMenuVisible"
}
--------
There is also an extension presented as a fix:
Keyboard QuickFix, but it shouldn't be necessary anymore.
Go to "Keyboard shortcuts" and search for selectNextSuggestion.
You can rebind this command to any shortcut you like.
Same goes for selectPrevSuggestion.
I use cmd + j for up and cmd cmd + k for down ^^

VS Code: Single keybinding to Spit/Merge Editor?

Is there a way to detect the split/single states of the editor so I can use a single keybinding to split and merge an editor?
{
"key": "ctrl+\\",
"command": "workbench.action.splitEditorDown"
"when": "EDITOR IS IN SINGLE STATE"
},
{
"key": "ctrl+\\",
"command": "workbench.action.editorLayoutSingle",
"when": "EDITOR IS IN SPLIT STATE"
}
Or perhaps there is a different way to set this up?
Thanks.
I think this what you are looking for:
{
"key": "ctrl+\\",
"command": "workbench.action.splitEditorDown",
"when": "!multipleEditorGroups"
},
{
"key": "ctrl+\\",
"command": "workbench.action.editorLayoutSingle",
"when": "multipleEditorGroups"
}
I found that when clause by using the Developer: Inspect Context Keys command.
(1) open the Developer Tools Console: Help/Toggle Developer Tools
(2) run the above command from the Command Palette and click anywhere in an editor
(3) that will create a large object in the Developer Tools Console that has a lot of when context clauses with their current values
(4) do a find in the Console for groups and look for a promising when clause (I first searched with the term split but nothing was helpful).
There is nothing to tell you if a specific file is split into two (or more) groups though - only that there are 1 or more groups of editors.

VSCode: Open files NOT in a new tab, reuse current tab

I believe that this is not covered by the Preview feature. I simply want to open a file for editing via Quick Open (or any way?) and replace the contents of the active tab, closing the open file and replacing it with the new one.
This behavior is central to the way I edit. Currently, I'm always opening new tabs that I don't want. It's the only barrier left between Code and the way I've used Vim for 15 years. I imagine that this is scriptable, but would like to avoid going down that road. Please tell me I'm missing something.
(1) The drastic approach: search for these in your settings:
Workbench > Editor > Limit: Enabled enable this
Workbench > Editor > Limit: Value set to 1
Drastic, because it will limit you to only 1 editor tab, probably not what you want but it does reuse the active (and only tab) of course.
(2) The macro approach:
Using a macro extension like multi-command put this into your settings.json
"multiCommand.commands": [
{
"command": "multiCommand.openFileInActiveEditor",
"sequence": [
"workbench.action.closeActiveEditor",
"workbench.action.acceptSelectedQuickOpenItem",
"workbench.action.closeQuickOpen" // if you want to close the quickopen panel immediately
]
}
]
and in keybindings.json:
{
"key": "alt+0", // whatever you want
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.openFileInActiveEditor" },
"when": "inFilesPicker && inQuickOpen"
},
It appears that you cannot override the usual right keybinding from the quickOpen panel so I set it to alt+right instead but you can pick whatever you want.
#Mark's answer almost gets you there, but it doesn't work with new (one tab) panes. Here's a modified version of his settings.json edit that does.
Install the multi-command extension
Put this in settings.json
"multiCommand.commands": [
{
"command": "multiCommand.openFileInActiveEditor",
"sequence": [
"workbench.action.acceptSelectedQuickOpenItem",
"workbench.action.previousEditor",
"workbench.action.closeActiveEditor",
"workbench.action.closeQuickOpen"
]
}
]
Put this in keybindings.json and replace the dummy value for the key key with your desired key combination
{
"key": "some+key+combination",
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.openFileInActiveEditor" },
"when": "inFilesPicker && inQuickOpen"
},