WillHaslett
WillHaslett

Reputation: 738

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

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.

Upvotes: 7

Views: 3842

Answers (5)

Miroslav Hrivik
Miroslav Hrivik

Reputation: 892

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

"workbench.editor.enablePreviewFromQuickOpen": true

enter image description here

Upvotes: 0

Brandon
Brandon

Reputation: 1

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

Upvotes: 0

Number 3434
Number 3434

Reputation: 34

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

Upvotes: 0

Mark
Mark

Reputation: 182661

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

Upvotes: 10

WillHaslett
WillHaslett

Reputation: 738

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

Upvotes: 2

Related Questions