简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM