Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

visual studio code - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...