0

How can I open a specific file in a specified split view with an built-in command in VS codium? Is that possible without writing an extension?

I have a project, and I periodically I want to open a pair of files in specific way in split view. Let's mention them as problem1.py and problem1.txt. I want to programmatically open problem1.py in the left side, and the problem1.txt in the right side.

I found an a documentation for the command vscode.open:

vscode.open - Opens the provided resource in the editor. Can be a text or binary file, or an http(s) URL. If you need more control over the options for opening a text file, use vscode.window.showTextDocument instead.

  • uri - Uri of a text document
  • columnOrOptions - (optional) Either the column in which to open or editor options, see vscode.TextDocumentShowOptions
  • label - (optional)
  • (returns) - no result

In keybindings.json I created following statements:

    {
        "key": "numpad4",
        "command": "vscode.open",
        "args": "/home/user/myproject/README.md"
    },
    {
        "key": "numpad6",
        "command": "vscode.open",
        "args": ["/home/user/myproject/README.md", "1"]
    },

Now when I press numpad4, it works perfectly, the readme file opens. But when I press numpad6, I get a notification:

Unable to open '': An unknown error occurred. Please consult the log for more details.

Am I passing parameters in a wrong way? Why it does not detect a filename? And I do not see whare to view a log.


Additional info:

VS codium version: 1.66.2.

I saw a cli option -r, --reuse-window, but it has not control of in which view I want to open a file.

I saw a similar question, but there the author wants to do it from extension, while I would prefer to not write an extension for this problem. Also, as documentation says, I think I do not need vscode.window.showTextDocument, as vscode.open should be enough for my task.

Here is an enum list for available ViewColumn values: https://code.visualstudio.com/api/references/vscode-api#ViewColumn

5
  • 1
    you can use the extension HTML Related Links use command htmlRelatedLinks.openFile
    – rioV8
    Commented May 5, 2022 at 16:55
  • Looks like exactly what I needed. I see you are an author of that extension. Still interested why that extension is needed. Can you please make a proper answer so I can upvote? Also, why does it ignore "viewColumn": "1" and "viewColumn": "2"? It always opens document in a current view.
    – Ashark
    Commented May 5, 2022 at 19:16
  • 1
    don't give it a string value but a numeric value: "viewColumn": 1, maybe have to adjust the README to make it clear
    – rioV8
    Commented May 5, 2022 at 21:01
  • 1
    I think the vscode.open command was not really intended for use outside an extension. Note that the first argument is a uri which you wouldn't normally have easy access to except in an extension. [That was later relaxed to a simple path string, but the documentation was never updated.] This is why there are a number of extensions to do this, but HTML Related Links appears to handle the viewColumn arg whereas some others don't.
    – Mark
    Commented May 5, 2022 at 21:33
  • Thanks, replacing string to numeric value (deleting quotes) worked.
    – Ashark
    Commented May 6, 2022 at 6:46

1 Answer 1

0

Combining the @rioV8's solution of using HTML Related Links and @Mark's solution for combining two commands into one, the resulting keybindings.json is the following:

    {
        "key": "numpad5",
        "command": "extension.multiCommand.execute",
        "args": {
            "sequence": [
                {   "command": "htmlRelatedLinks.openFile",
                    "args": {
                        "file": "/home/user/myproject/problem1.py",
                        "method": "vscode.open",
                        "viewColumn": 1,
                    }
                },
                {   "command": "htmlRelatedLinks.openFile",
                    "args": {
                        "file": "/home/user/myproject/problem1.txt",
                        "method": "vscode.open",
                        "viewColumn": 2,
                    }
                },
            ]
        }
    },

Not the answer you're looking for? Browse other questions tagged or ask your own question.