Can vscode open files with external programs? - visual-studio-code

Is there any way to set vscode such that double-clicking a .docx file in the embedded file explorer causes the file to be opened in Microsoft Word?
Context
I'm used to PyCharm but I've been migrating to vscode. 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 vscode 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.

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)

To work, Windows users should take care with command quotes
tasks.json :
{
"label": "open file",
"type": "shell",
"command": "'c:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE' '${file}'"
}

Related

How to configure VSCode to automatically open a text file when I open a saved workspace?

I am new to VSCode, just started using workspaces.
I was wondering if there is a way to configure my workspace (named fun_projects), to automatically open two text files, as I open the workspace. Like as I open the workspace, I want two previously saved text files to open
You can make a task like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "Open two files on workspace open or reload",
"type": "shell",
// -r flag re-uses the current vscode window
// note the double-escaping for this form of backslashed paths
"command": "code -r 'C:\\Users\\Mark\\OneDrive\\folder-operations\\providers.js' 'C:\\Users\\Mark\\OneDrive\\folder-operations\\test.json'",
// the above can be shortened with the use of a variable:
"command": "code -r '${workspaceFolder}\\providers.js' '${workspaceFolder}\\test.json'",
"problemMatcher": [],
"runOptions": {
"runOn": "folderOpen" // makes this task run on folder open
}
}
]
}
That goes into your tasks.json file in the .vscode folder at the root of your workspace.
It isn't the fastest operation as other vscode set-up occurs first, but it works.
For the Insiders Build, you would use code-insiders -r ....

How to open file using vscode tasks in remote vscode

I'd like to be able to use the new vscode tasks feature to open a file in remote vscode. This is useful because I have a bunch of symlinked log files in tmp that I would like to open, and I don't want to type their whole name every time. Ideally it would be something like this:
{
"label": "$(play) open log file",
"type": "shell",
"command": "code /tmp/logfile.INFO",
"group": "build",
},
If I run the same command in the normal vscode terminal, it opens the file correctly. If I run it in the task, the environment doesn't have vscode's bin in the path. If I change code in the task to use the direct path of the remove vscode, it then complains saying "Command is only available in WSL or inside a Visual Studio Code terminal."
I'm unclear if this fails because it is remote vscode or if it fails because it's not possible to open code in a task. Either way it'd be nice to do so :)

Shortcut to run code via integrated terminal

Currently Ctrl+Enter runs code from the main directory.
I want to change this shortcut to run code from the integrated terminal directly.
My current workflow is:
right click file.py
Open in integrated terminal
type python file.py
Is there way to change the shortcut above to use the workflow instead.
If you want, you can make a task and assign a keybinding to it.
If you don't have a tasks.json file, go to the Command Palette (Ctrl+Shift+P if that hasn't been remapped), and enter Tasks: Configure Tasks, Configure tasks.json file from template, and Others. You should now have a tasks.json file.
The tasks key is an array, and you can insert an object there. For your case, you can have
{
"label": "Run python file",
"type": "shell",
"command": "py",
"args": ["${file}"],
"group": {
"kind": "build",
"isDefault": false
}
}
To have the keybinding, go to Keyboard Shortcuts (Settings > Keyboard Shortcuts [Ctrl + K Ctrl + S if this hasn't been remapped]) and click Open Keyboard Shortcuts (JSON).
There, define your own keybinding. For now, I choose F5F5, and we'll add this object.
{
"key": "f5 f5",
"command": "workbench.action.tasks.runTask",
"when": "editorLangId == python",
"args": "Run python file"
}
To explain, when we press F5 followed by F5 and we are an editor identified as a Python file, VSCode will run the command workbench.action.tasks.runTask. To choose which task VSCode will run, we'll provide the name of the task through the args key which is Run python file.

How to pass arguments to existing vscode commands

I would like to be able to run an existing vscode command or extension and pass arguments to it. From my research so far this is what I would expect the vscode task.json to look like, but it does not work. What am I doing wrong?
{
"label": "open index html in editor",
"command": "${command:workbench.action.files.openFile}",
"args": ["/home/host_user/src/index.html"]
},
{
"label": "liveView index html",
"command": "${command:extension.liveServer.goOnline}",
"args": ["/home/host_user/src/index.html"]
},
If I run the file command it opens a vscode prompt to select a file. If I try to liveView the file, it runs live view on my root workspace (as no path is getting passed to it).
This is the extension I'm trying to open with file with:
https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer
Thanks

Make vscode open the panel on start-up

Every time I open vscode I have to click View->Appearance->Show panel or press Ctrl+J to open panel with console. Is it possible to run vscode with the panel open by default?
Vscode should remember whether the panel is open or not each time you close the window. But if it doesn't or you typically have it closed when you exit that vscode window and you have to Ctrl+J each time you open vscode try putting this task into your tasks.json:
{
"label": "open Panel",
"command": "${command:workbench.action.togglePanel}",
"type": "shell",
"problemMatcher": [],
"runOptions": {"runOn": "folderOpen"}
},
Now whenever the workspace opens, the togglePanel command will run automatically.