Home > Software design >  What is the command to open a file in specified ViewColumn in VS Codium?
What is the command to open a file in specified ViewColumn in VS Codium?

Time:05-06

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

CodePudding user response:

you can use the extension HTML Related Links use command htmlRelatedLinks.openFile

  {
    "key": "numpad6",
    "command": "htmlRelatedLinks.openFile",
    "args": {
      "file": "${workspaceFolder}/README.md",
      "method": "vscode.open",
      "viewColumn": 1
    }
  }

CodePudding user response:

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,
                    }
                },
            ]
        }
    },
  • Related