Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode Open new view into file

We can use the "split editor" option to make two views into one file.

I'm looking for an option to open the same file in separated tabs like I can do in Sublime Text (open new view of file). Is that possible?

Note: I want to do this without splitting the view, so there should be two tabs for the same file within the same view container.

like image 205
ADIN Avatar asked Apr 12 '18 11:04

ADIN


People also ask

How do you open a file in a new window VS Code?

On Windows and Linux, press CTRL + K , then release the keys and press O (the letter O, not Zero). On macOS, press CMD + K , then O (without holding CMD ). This will open the active file tab in a new window/instance.

How do I open view in VS Code?

The most important key combination to know is Ctrl+Shift+P, which brings up the Command Palette. From here, you have access to all of the functionality of VS Code, including keyboard shortcuts for the most common operations.

How do I open preview to the side in VS Code?

To switch between views, press Ctrl+Shift+V in the editor. You can view the preview side-by-side (Ctrl+K V) with the file you are editing and see changes reflected in real-time as you edit.


1 Answers

I couldn't find anything built-in that lets you do this, nor an existing extension in the marketplace. I thought it should be quite trivial to implement a "Duplicate Tab" command yourself in a custom extension, but it turns out VSCode only allows the same resource to be opened once within the same view column.

It's still possible to do this on Windows or macOS, but only by abusing this bug:

Issues with not case/fragment-normalizing file paths (macOS, Windows) #12448

Here's what the code for the extension looks like:

'use strict';
import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    vscode.commands.registerCommand("duplicateTab", () => {
        var activeEditor = vscode.window.activeTextEditor;
        if (activeEditor == null) {
            return;
        }
        // HACK!
        const sameFileNameButDifferent = activeEditor.document.fileName.toUpperCase();
        vscode.workspace.openTextDocument(sameFileNameButDifferent).then(document => {
            vscode.window.showTextDocument(document, {preview: false});
        });
    });
}

In package.json:

"contributes": {
    "commands": [
        {
            "title": "Duplicate Tab",
            "command": "duplicateTab"
        }
    ]
},

like image 132
Gama11 Avatar answered Oct 06 '22 08:10

Gama11