visual-studio-codevscode-extensions

Can VS Code open files with external programs?


Is there any way to set VS Code such that double-clicking a .docx file in the embedded file explorer causes the file to be opened in Microsoft Word?

I'm used to PyCharm but I've been migrating to VS Code. I'm used to creating Word document files (.docx) and double-clicking them in the file explorer on the left side sub-window to launch Word and see what the document looks like. This works in PyCharm, but in VS Code it tries to open the file as a binary and claims it has no editor. Even if it did had an editor, I wanted it to open in my second monitor (or at least to be able to move it to my second monitor). For the time being, I'm opening a file explorer window and double clicking the file there, which has been less than optimal.


Solution

  • It's not quite double clicking, but you could create an open file command as a task, then bind that task to a keyboard shortcut, which amounts to one click and one hotkey.

    Creating the open file task

    We preferably make this task user-level, so that is accessible in every project.

    ctrl+shift+p and search Tasks: Open User Tasks

    In the tasks.json file, add the open file task to the tasks array:

    // Inside "tasks": [ ... ]
    {
      "label": "open file",
      "type": "shell",
      "command": "${file}",
      "presentation": {
      // Hides the task terminal from showing up
        "reveal": "never",
      }
    }
    

    Change the "command" to whatever the open file command for your shell is (just ${file} works for windows, but change it to xdg-open ${file} for linux)

    Assigning a keybinding to the open file task

    To bind the task command to a keybinding, ctrl+shift+p and search keyboard shortcuts (JSON), open the file. Add the following to the array (changing "key" to whatever combination you desire)

    // [ ... ]
    {
      "key": "ctrl+alt+l",
      "command": "workbench.action.tasks.runTask",
      "args": "open file"
    }
    

    To open any file, click on it to focus it in a tab (which sets the ${file} defined in tasks.json) and use the assigned hotkey. (As a side note, opening files that have vscode as their default program will open it in the same instance, so it makes it seem like the task isn't doing anything)