How to open file from VSCode file explorer in same window - visual-studio-code

Using cmd+shift+e I can highlight the file explorer tab and then use vim keybindings to select a given file. However, I've only figured out how to open a selected file in a new editor pane (Ctrl+Enter).
Is there a shortcut to open it in the same pane? Can I change the behavior of the Ctrl+Enter shortcut? A pointer to the right docs would also be helpful.

This closest action is filesExplorer.openFilePreserveFocus, which is bound by default to Space.
This will open the file currently selected in a new tab, but it won't move your cursor to it.
You can use the cmd+shift+e keybinding to get back to your editor, though!

From the docs
The settings window.openFoldersInNewWindow and window.openFilesInNewWindow are provided to configure opening new windows or reusing the last active window for files or folders and possible values are default, on and off.
If configured to be default, we will make the best guess about reusing a window or not based on the context from where the open request was made. Flip this to on or off to always behave the same. For example, if you feel that picking a file or folder from the File menu should always open into a new window, set this to on.
Note: There can still be cases where this setting is ignored (for example, when using the -new-window or -reuse-window command-line option).

The filesExplorer.openFilePreserveFocus command does what you want with the limitation that it keeps the focus on the explorer sidebar. To move focus to the opened window you can utilize the fact that workbench.view.explorer toggles focus between editor and explorer. Together with a extension such as multi-command that enables running multiple commands the desired behaviour can be achieved:
{
"key": "enter",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"filesExplorer.openFilePreserveFocus",
"workbench.view.explorer",
],
},
"when": "explorerViewletFocus && explorerViewletVisible && !inputFocus"
},

Related

VS Code Each time when I select another file and start to type, I will type in the side bar, not on a page. How to fix it? [duplicate]

I realize that Microsoft had some sort of reason for adding the new "Find" tool in the folder EXPLORER section of VSC.. but... I'm a creature of habit. When I click on a file and then press Ctrl+F, I immediately start typing the value I'm looking for. The results used to look like this in the file editor pane...
But with my last update, when I click on the file in the EXPLORER and press Ctrl+F, I am now getting this NEW small tool in the EXPLORER pane, and the cursor goes THERE. I type away and nothing happens in the file editor until I swear a few near-curse-words and have to click over in the file editor and then press Ctrl+F again and start all over to type the search string. It's bugging me because its old habit.
How can I go back to the old way of how it worked? Is there a simple configuration buried somewhere I can change?
Thanks.
I don't say see a setting to set the old filter-search method as the default in the Explorer. You can disable the list.find command on which the new find widget in the Explorer depends in your keybindings.json which has the effect you want:
{
"key": "ctrl+f",
"command": "-list.find",
// "when": "listFocus && listSupportsFind"
}
Now Ctrl+F with focus in the Explorer will open the editor Find Widget with focus.
But you lose the ability to filter other lists with the Ctrl+F, such as TreeViews (see https://stackoverflow.com/a/73039598/836330 forexample).
The better solution, IMO, is to set up a macro which works when you have explorerFocus and use the Ctrl+F keybinding. You will need a macro extension, like multi-command. Use this keybinding in your keybindings.json:
{
"key": "ctrl+f",
"command": "extension.multiCommand.execute",
"args": {
"sequence": [
"workbench.action.focusActiveEditorGroup",
"actions.find",
],
"when": "explorerFocus"
},
}
which will switch focus to your current editor and then open the Find Widget therein.
I will provide three steps to achieve this without editing keybindings.json
Step 1: Open keyboard shortcuts
Step 2: Find list.find using the search bar.
Step 3: Change key-binding to f3. If there are multiple bindings f3 and ctrl+f like it was for me, remove ctrl+f key-binding.
Done.
You can open keyboard shortcuts by:
ctrl + k ctrl + s
Open palette ctrl + shift + p, type open keyboard shortcuts.

VSCode "Quick Open" menu alters "recently used" ordering too eagerly

When I step through the the VSCode Quick Open menu, I want it not to automatically switch between editors until I've selected the file I'm interested in (and hit enter). But with a recent (April?) change to VSCode, it automatically shows me each file as I step through the menu. How can I disable that behavior?
Here's my motivation, for those who are interested:
One of the (common?) ways to use VSCode's "Quick Open" menu is to use it like the "last channel" button on a TV remote. I want to open one editor, then open another, and then quickly toggle back and forth between the two. Since they are my current and most-recent editors, they should appear at the top of the Quick Open Menu, so toggling between them is fast.
This used to work just fine, but a recent (April?) change to the Quick Open menu seems to have broken this functionality. Now, it automatically activates the editor for every file that I step through in the menu, changing my "most recent" ordering. It can no longer be used like a "last channel" button.
Here's a short video demonstrating the problem. Here, I'm starting with one.txt. I want to select five.txt via the Quick Open menu, and then quickly toggle between the two. But it opens all the other files along the way!
(In fact, if you watch closely, the behavior is even weirder: It doesn't show me the currently selected file -- it opens the editor for each file as it becomes UN-selected. Maybe this is just a bug?)
FWIW, here are the the relevant parts of my keybindings.json, to make it clear which commands I'm referring to:
// keybindings.json
[
// Trigger quick open menu and pre-select the previously used editor.
// (With either cmd+left/cmd+right)
{
"key": "cmd+left",
"command": "workbench.action.quickOpenPreviousRecentlyUsedEditorInGroup",
"when": "!inEditorsPicker"
},
{
"key": "cmd+right",
"command": "workbench.action.quickOpenPreviousRecentlyUsedEditorInGroup",
"when": "!inEditorsPicker"
},
// Once the menu is open, scan through the choices.
// (Forward with cmd+right, backward with cmd+left)
{
"key": "cmd+left",
"command": "workbench.action.quickOpenNavigatePreviousInEditorPicker",
"when": "inEditorsPicker && inQuickOpen"
},
{
"key": "cmd+right",
"command": "workbench.action.quickOpenNavigateNextInEditorPicker",
"when": "inEditorsPicker && inQuickOpen"
}
]
The issue is that my chosen key binding conflicts with a hard-coded key binding in VSCode. A workaround is to just use a key other than the right arrow.
Explanation here: https://github.com/microsoft/vscode/issues/98479#issuecomment-633378132

VSCode: keep focus in folders panel after selecting a file

I want to be able to navigate files in VSCode workspace efficiently via keyboard: exploring the repo by looking at dozens of files before I find a good one.
I can highlight some file in the Folders view by clicking it, then I use arrows to navigate around (so far so good), but when I click Enter, the file is opened and the editor takes focus - which prevents me from navigating the tree further.
Is it possible to configure VSCode so that editor does not take focus after I open it from the sidebar?
The best I found so far is that it is possible to create a key binding to bring back the focus to explorer: ctrl-shift-p > Preferences: Open Keyboard Shortcuts > look for workbench.explorer.fileView.focus (by default there's no key binding assigned).
[EDIT}: As of v1.38 a new command has been added that allows you to open an explorer file and keep focus in the explorer:
Open file and preserve focus
Now it is possible to open the file in preview from the explorer while
preserving the focus in the explorer. The new command is
filesExplorer.openFilePreserveFocus and by default it can be triggered
via the space key.
from open file and preserve focus
[Original answer - now see edit above]
You can do this in one step with a macro extension which enables you to run two commands with the enter key. Here I am using the multi-command extension which has an interval delay option. A 150ms delay between the commands seems necessary to allow the file to be opened before switching focus back to the file explorer.
"multiCommand.commands": [
{
"command": "multiCommand.openFileKeepFocusInExplorer",
"interval": 150,
"sequence": [
"list.select",
"workbench.explorer.fileView.focus",
]
}
]
And then assign a keybinding (in keybindings.json) to that multi-command:
{
"key": "enter",
"command": "multiCommand.openFileKeepFocusInExplorer",
"when": "sideBarFocus && activeViewlet == 'workbench.view.explorer'"
},
In the demo below, I am only using the arrow and enter keys to move between files, open them and keep focus in the file explorer:

How to reset Visual Studio Code key bindings?

I have been experimenting with my vs-code key bindings.
I would like to reset the key-bindings to the original settings.
How do I do that?
I am on Linux Mint 18.
I tried removing all the records from the keybindings.json
Version 1.34.0 seems to have the settings at a slightly different location:
Click File > Preferences > Keyboard Shortcuts
There is a triple-dot (...) at the top-right hand corner. Click on that and select "Show User Keybindings"
Delete your listed keybindings
Here are the steps to reset the keybindings in VS code.
Click File > Preferences > Keyboard Shortcuts or Press Ctrl+K Ctrl+S
Then, click on keybindings.json
From keybindings.json remove the custom bindings you want to reset.
It seems newer versions of VSCode (>1.33 for Mac) doesn't have a direct link to keybindings.json anymore, as this answer shows. However, there is an option to reset user defined keybindings without mess with files.
Go to the Keyboard shortcuts settings:
There, find the setting tagged as "User". If you click on it with the right mouse button, a context menu will show the option "Reset Keybinding":
This action is gonna reset the selected keybinding and tag it with "Default" again.
first go file > preferences > keyboard shortcuts
you can see all key that you change whit click on triple-dot
or put ( #source:user ) in search bar
now you can right click on which one that you want to reset and select ( reset keybinding )
If you had installed the Keybinding as an Extension e.g Sublime or IntelliJ IDEA Keybindings, simple go to Extension and disable or uninstall it and you would have your default keybinding.
Do we need another answer? Maybe not, but every year or so I find myself sifting through the information on this page, so to make it quicker next time, here are some notes:
To find the location of the settings, you can look for a button/link to the json file located somewhere in Preferences. However, I have found it easier to find the json files on my hard drive than to locate that button/link inside the app (some users report that the button/link is missing in some versions of the app). If your OS does not allow you to search through system files, open a terminal session and type $ locate keybindings.json.
If you can memorize shortcuts, a typical default shortcut that can take you to the button/link is CMD+SHIFT+P. This shortcut opens a box below the main toolbar and you can type "json" in that box to find a button/link to the json file.
General settings are in settings.json
Keyboard settings are in keybindings.json
MacOS: ~/Library/Application Support/Code/User/
Example of keybindings.json
// Place your key bindings in this file to override the defaultsauto[]
[
{
"key": "cmd+r cmd+r",
"command": "workbench.action.reloadWindow",
"when": "isDevelopment"
},
{
"key": "cmd+r",
"command": "-workbench.action.reloadWindow",
"when": "isDevelopment"
},
{
"key": "shift+cmd+c shift+cmd+c",
"command": "workbench.action.terminal.openNativeConsole",
"when": "!terminalFocus"
},
{
"key": "shift+cmd+c",
"command": "-workbench.action.terminal.openNativeConsole",
"when": "!terminalFocus"
},
{
"key": "ctrl+cmd+c",
"command": "editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+shift+alt+cmd+[Minus]",
"command": "-editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "shift+cmd+c",
"command": "editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "shift+alt+a",
"command": "-editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
}
]
Note that mapping a key combination that is already in use may result in conflicts. So the best approach is to first remap that default binding to something else. In the above, for instance, the "-" that prefixes "-editor.action.blockComment" serves to suppress the default binding. Thus, you may find that your key bindings are best set in pairs (unless your preferred combinations are sufficiently rare).
Example of settings.json
{
"workbench.colorTheme": "Solarized Light",
"window.zoomLevel": 4,
"workbench.activityBar.visible": false,
"workbench.statusBar.visible": false,
"editor.quickSuggestions": false,
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.acceptSuggestionOnCommitCharacter": false
}
Try this documentation page about key binding in VSCode:
https://code.visualstudio.com/docs/getstarted/keybindings
Open a directory that contains user settings (https://code.visualstudio.com/docs/getstarted/settings) and try to remove user key bindings file.
On VS Code version 1.42.1 on Mac you can find a button that opens the Keyboard shortcuts JSON file on the top right corner of the keyboard shortcuts screen which you can open from Code -> Preferences -> Keyboard Shortcuts
For the newer version of VSCode (Version: 1.43.1), you can open the keybindings.json file from the Command Palette (⇧⌘P OR Ctrl+Shift+P) with the Preferences: Open Keyboard Shortcuts (JSON) command.
Once you delete all the data in the keybindings.json file you should get rid of any changes you made to keyboard shortcuts for your installation. Everything would be set back to default.
Reason: The first line in keybindings.json file is a comment // Place your key bindings in this file to override the defaultsauto[], which means if you delete all what is there you'll get the VSCode defaults. (Ref https://code.visualstudio.com/docs/getstarted/keybindings#_advanced-customization)
You can find all information on keybindings here.
For future searchers, since this question refers to Linux, even if the keybindings.json file is moved again one can always just use locate to find it:
$ locate keybindings.json.
Chances are, you will only have one, and if you have more, it will be clear where it is, as it is somewhere inside Code folder.
For example, as of today, mine is here: /home/auser/.config/Code/User/keybindings.json
Going directly to the file, will give you the opportunity to keep what you want and remove what you think might be the problematic setting.
For VSCode Version 1.35.1, which I'm using, one can directly open the keybindings.json file using the button that looks like {} on top-right corner of "Keyboard Shortcuts" Tab's title bar:
Picture showing {} button in top-right corner
Cleaning content of this file cleans all user defined key bindings.
In the latest version, the setting json file is with highlighted button.
I deleted everything in there and it seems to reset all keys.
User setting file
If you are on a Mac, press and hold command while hitting the k and s keys. Then click the icon on the top right with the three circles and press "Show User Keybindings". Then, hit command + delete while highlighting over the keybinding you want to delete.

Toggle between fullscreen editor and terminal in VS Code

As a Windows systems admin, I use PowerShell quite a lot. With the release of PS Core, and the implication that the ISE is dead, I've started to try to use VS Code as my day to day tool. One feature I'm missing from ISE is the ability to swap between the editor and the terminal in fullscreen. I usually kept ISE open and maximized, and used Ctrl+R to swap between editor and terminal as needed. I haven't found a way to maximize the terminal, and swap easily between terminal and editor. I know I can make the terminal take up most of the screen, but a) this still leaves about 2 lines of editor open at the top, and b) there doesn't seem to be an easy way to then maximize the editor. Is there a way to minic the ISE behaviour that I haven't found yet?
To toggle between a full screen editor and a nearly full screen terminal you can use:
{
"key": "ctrl+alt+m",
"command": "workbench.action.toggleMaximizedPanel"
}
with your keybinding of choice to replace the Ctrl-Alt-m : that is mine. You just need to "maximize" the terminal first - pull it up as far as it goes. It will remember that between sessions.
Revisiting this :
As of v1.38 this is now pretty simple. There are two commands that will toggle the panel/editors to full screen.
Pick some keybinding to use for the toggle trigger:
{
"key": "ctrl+alt+q",
"command": "workbench.action.toggleMaximizedPanel",
// "command": "workbench.action.toggleEditorVisibility" either one
"when": "!terminalFocus"
},
The above will expand the panel or editor to full height, but toggling back will return the panel to its original size but not to nothing. If you want the terminal to bounce between full open and full closed try both of these keybindings:
{
"key": "ctrl+alt+t", // you could use "key": "ctrl+`", if you wish
"command": "workbench.action.closePanel",
// "when": "terminalFocus"
},
{
"key": "ctrl+alt+t",
"command": "workbench.action.toggleMaximizedPanel",
"when": "!terminalFocus"
},
The order of the above 2 keybindings is important.
v1.50 is adding the setting panel.opensMaximized - I tried its different options but couldn't get a simpler result than the two keybindings ctrl+alt+t version I showed above. In any case, start with the panel closed for it to work well.
The below outlines my solution after reading #Mark 's answer, as it's slightly different. I use ctrl-alt-m to switch between a full-sized terminal and full-sized editor.
note: this includes the mentioned integrated terminal and vscode application menu bars.
For posterity's sake, I'm on vscode version 1.40.1.
Implementation
You'll need to add to your keybindings within vscode and execute a manual step.
keybindings.json
Add this to your keybindings.json file, accessible via the Keyboard Shortcuts editor:
{
"key": "ctrl+alt+m",
"command": "workbench.action.toggleMaximizedPanel",
"when": "!terminalFocus"
},
{
"key": "ctrl+`",
"command": "-workbench.action.terminal.toggleTerminal",
"when": "!terminalFocus"
},
{
"key": "ctrl+alt+m",
"command": "workbench.action.terminal.toggleTerminal",
"when": "terminalFocus"
}
Slide down the integrated terminal
Once you've done this and saved your keybindings.json file you need to manually slide down the integrated terminal all the way off the screen on the bottom, after opening from your editor with ctrl+`.
Afterwards, you should be able to use ctrl+alt+m in your editor and your terminal to get a full-screen-ish experience moving between them.
I've tested this on Ubuntu and Fedora locally as hosts and using remote-ssh to an Ubuntu remote from a Windows 10 host. This has the added benefit of allowing you to get the by-default smaller terminal from within your editor using ctrl+` but use a single command, ctrl+alt+m, for switching between editor/terminal. YMMV!
Solution 1: Create Terminal in new Tab and Switch Between Tabs
Ctrl+Shift+P > Terminal: Create New Terminal in Editor Area create a terminal as a new Tab (A.k.a. Editor). It looks like:
Now you can toggle between Tabs (A.k.a. Editors) and Terminal (Which is in a new Tab) using View: Quick Open Previous Recently Used Editor in Group
Solution 2: Create Terminal in new Editor Group and Switch Between Editor Groups
Ctrl+Shift+P > Terminal: Create New Terminal in Editor Area to the Side create a terminal in new Editor Groups. It looks like:
Now you can toggle between window and terminal using View: Navigate Between Editor Groups
NOTE:
You can add keybinding to the commands as per your convenience. For example, in case of Solution 1:
{
"key": "ctrl+`",
"command": "workbench.action.createTerminalEditor"
},
{
"key": "ctrl+tab",
"command": "workbench.action.quickOpenPreviousRecentlyUsedEditorInGroup",
"when": "!activeEditorGroupEmpty"
},
{
"key": "ctrl+w",
"command": "workbench.action.terminal.killEditor",
"when": "terminalEditorFocus && terminalFocus && terminalHasBeenCreated && resourceScheme == 'vscode-terminal' || terminalEditorFocus && terminalFocus && terminalProcessSupported && resourceScheme == 'vscode-terminal'"
}
Here, Ctrl+W & Ctrl+Tab are set by default. I just modified Ctrl+`.
The above keybinding will
Create terminal when you press Ctrl+`
Close terminal when you press Ctrl+W
Toggle between fullscreen editor and terminal when you press Ctrl+Tab
Opening the terminal in VS Code in full screen
This solution will replace the standard keybindings in VS Code so the terminal toggles in full screen.
Implementation
Open command palette ctrl+shift+p and search for Preferences: Open Keyboard Shortcuts (JSON) and paste this between the square brackets.
{ "key":"ctrl+j", "command":"workbench.action.toggleMaximizedPanel","when":"!terminalFocus" },
{ "key": "ctrl+oem_3", "command":"workbench.action.toggleMaximizedPanel", "when":"!terminalFocus" }
There is now an extension for exactly this. It is the "maximizeterminal" extension by Samuel T Scott. It redefines ctrl+` for opening terminal full-screen. The above keybinding methods, are also great, but in my experience they tend to not work properly once you start opening files by using cli like "code app.js". While using the extension such problems do not occur.
For macOS users, you can set the shortcut in the same keybindings.json file.
You can access the file using the shortcut cmd+shift+p, then write/select Preferences: Open Keyboard Shortcuts (JSON).
In keybindings.json, paste the following code between the square brackets:
{ "key": "cmd+alt+m",
"command": "workbench.action.toggleMaximizedPanel",
"when": "!terminalFocus" }
You can change the shortcut to whatever suits you (mind rewriting any of the default ones!), but I set mine to the same as others suggested (cmd+option+m) to keep a "standard".
Right click on the terminal instance name at the top as seen below and then select "move terminal into editor area". This will allow you to have a full workspace view within VS Code.
Image of process
I love this question, I believe I figured out something or at least got the behavior I was looking for.
Using the workspace setting here: https://github.com/microsoft/vscode/issues/107624
(enter ctrl+, and search "openmaximized" and set the dropdown to "always")
From there I set the keyboard shortcut for "Toggle Terminal" to ctrl+j (or whatever you may prefer) and now when I toggle my terminal it goes from full screen to toggled off
For MacOS I found useful cmd+option+m