VSCode how to know when editor is not split? - visual-studio-code

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.

Related

How to recover the editor area after executing "workbench.action.toggleEditorVisibility" in VSCode?

I assigned ctrl+' ctrl+e as a keybinding to the VSCode action workbench.action.toggleEditorVisibility. After executing the keybinding once, the editor area disappeared. But, when I executed the keybinding once again, the editor area didn't return.
I was trying out some keybindings in my keybindings.json file:
[
{
"key": "ctrl+' ctrl+m",
"command": "workbench.action.toggleMenuBar"
},
{
"key": "ctrl+' ctrl+a",
"command": "workbench.action.toggleActivityBarVisibility"
},
{
"key": "ctrl+' ctrl+s",
"command": "workbench.action.toggleStatusbarVisibility"
},
{
"key": "ctrl+' ctrl+b",
"command": "workbench.action.toggleAuxiliaryBar"
},
{
"key": "ctrl+' ctrl+p",
"command": "workbench.action.toggleSidebarPosition"
},
{
"key": "ctrl+' ctrl+t",
"command": "workbench.action.toggleTabsVisibility"
},
{
"key": "ctrl+' ctrl+e",
"command": "workbench.action.toggleEditorVisibility"
},
]
As you can see all of them have to do with toggling UI elements. It was all going well until I reached the workbench.action.toggleEditorVisibility one. It didn't behave like a toggle command at all, it just got rid of the editor area all together to never bring it back. The other keybindings worked just fine after the editor area disappeared (except the workbench.action.toggleTabsVisibility one, since it depends on the editor area being there).
As a matter of fact, what the ctrl+' ctrl+e keybinding kept doing after the editor area disappeared was bringing up the "panel" (where the 'problems', 'output', 'debug console', and 'terminal' sections are) without focus, it wasn't toggling it, it would just bring it up and that's it. If I took it down with ctrl+j then ctrl+' ctrl+e would bring it up unfocused again.
This problem just affected the current VSCode session, thus, I could open other project with normality, but if I tried to open the same project in which I executed the previous commands, then the VSCode window became unresponsive at the beginning (sometimes with a warning message) but after waiting a few seconds it rendered in the same state as before: without the editor area.
Does anyone have any idea of what could be causing this problem, and more importantly, how to solve it?

How can I get line break on enter even when the search field is open (but not focused) in VS Code?

If I have the search field open and press enter in my code it automatically tries to search instead of giving me a line break. I've heard that there is a way to turn off this behavior and only search when the search field has focus. Anyone know how? Currently I have to hit ESC to close the search before I can add a line break and I forget to do it 95% of the time and my editor jumps to a different section of the code. Thanks!
Changing findWidgetVisible to findWidgetFocus in my keyboard shortcuts settings fixed it!
/**
* amVim Finder Fix
**/
{
"key": "enter",
"command": "editor.action.nextMatchFindAction",
"when": "findWidgetFocus"
},
{
"key": "shift+enter",
"command": "editor.action.previousMatchFindAction",
"when": "findWidgetFocus"
},

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"
},

visual studio code go to a tab by number

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).