How can I make VS Code replace the current tab instead of creating a new one when opening a file?
Asked Answered
E

5

7

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.

Engender answered 13/2, 2020 at 20:44 Comment(3)
I have the exact opposite problem. vscode replaces current tab instead of opening new tab. This only happens sometimes, seems random.Cisalpine
I agree with spinkus, very annoying VS Code is reusing tabs. If I wanted to close a file, I'd close a file. I think it is related to whether or not a file has been edited. Just disable workbench.editor.enablePreviewSubchloride
For those who end up here and want the opposite of the behavior desired in the OP, you may need to turn OFF Workbench > Editor:Enable Preview. If that setting is on, Code will replace the contents of your current tab when you open a file, unless you have edited the currently-selected open file.Engender
Y
10

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

Yugoslav answered 13/2, 2020 at 23:50 Comment(0)
E
2

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

  1. Install the multi-command extension

  2. Put this in settings.json

    "multiCommand.commands": [
      {
        "command": "multiCommand.openFileInActiveEditor",
        "sequence": [
          "workbench.action.acceptSelectedQuickOpenItem",
          "workbench.action.previousEditor",
          "workbench.action.closeActiveEditor",
          "workbench.action.closeQuickOpen"
        ]
      }
    ]
    
  3. 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"
    },
    
Engender answered 14/2, 2020 at 1:50 Comment(2)
Ah, I didn't test that - I don't frequently have only one tab open...Yugoslav
I was nowhere before your answer :)Engender
A
0

VSCode now has built-in support for multiple commands.

  • The multi-command extension is no longer required.
  • It seems that you can use the Tab key instead.
  • I have fixed and modified the answers to account for whether the current editor is in preview, and also so that it no longer requires the multi-command extension.

The keybindings.json file is now:

{
  "key": "tab",
  "command": "runCommands",
  "args": {
    "commands": [
      {
        "command": "workbench.action.closeActiveEditor",
        "when": "activeEditorIsNotPreview",
      },
      "workbench.action.acceptSelectedQuickOpenItem",
      "workbench.action.closeQuickOpen",
    ]
  },
  "when": "inFilesPicker && inQuickOpen",
},
Adularia answered 18/1, 2024 at 17:2 Comment(0)
F
0

For anyone stumbling upon this in the future, I think the best way to enable this workflow is to add these settings to your config:

  // Editor limits below only auto-close "clean" files so we must save
  // when losing focus to allow a new file to take its place
  "files.autoSave": "onFocusChange", 

  "workbench.editor.limit.enabled": true,
  "workbench.editor.limit.perEditorGroup": true, // allows splits
  "workbench.editor.limit.value": 1,
  "workbench.editor.showTabs": "single", // prettier

Some more conversation around these settings:

https://github.com/microsoft/vscode/issues/89275

https://github.com/microsoft/vscode/issues/144309

Fob answered 20/8, 2024 at 18:11 Comment(1)
this assumes that the user only wants one tab open in an given editor groupLanilaniard
U
0

This can be easily achieved by setting this values in settings.json

"workbench.editor.enablePreviewFromQuickOpen": true

enter image description here

Ultan answered 12/9, 2024 at 8:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.