Visual Studio Code 1.17.1 Open in Browser without opening terminal - visual-studio-code

Looks like an old question, but no proper answers found.
I've looked at here and here.
Right now, I can open Chrome by doing this:
task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "echo",
"type": "shell",
"command": "echo Hello"
},
{
"taskName": "Open in Chrome",
// "type": "process",
"windows": {
"command": "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
},
"args": ["${file}"],
}
]
}
keybindings.json:
[{
"key": "ctrl+alt+g",
"command": "workbench.action.tasks.runTask",
"args": "Open in Chrome"
},]
Note that I don't even need type: process to make it run and can only run it using my own key binding. If I use ctrl+shift+B (Windows), it'll allow one task only.
However, every time I run the task, the terminal is also opened with: Terminal will be reused by tasks, press any key to close it. which is repetitive and not really helpful for front-end work.
Is there a way to turn that off?
I've tried adding:
"presentation": {
"reveal": "never" //same with "silent"
}
to the task in task.json but it doesn't work.

Answer to close question:
The easiest way is to use an extension like this one: https://marketplace.visualstudio.com/items?itemName=techer.open-in-browser

Related

VSCode launch configuration to run python file without debugging

I understand that by creating a launch.json file in VSCode (slightly more cumbersome than pycharm), I can set debug configurations to launch individual python files.
For example:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Poker",
"type": "python",
"request": "launch",
"cwd": "C:/Users/dickr/git/poker",
"program": "C:/Users/dickr/git/Poker/poker/main.py",
"console": "integratedTerminal",
"env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
},
]
}
That all works great, but how can I run it in normal mode, without a debugger? How can this be defined in the launch configuration so I can select this in the dropdown, and potentially pass arguments with it?
Write a task in a tasks.json file. For documentation on tasks, see https://code.visualstudio.com/docs/editor/tasks#_custom-tasks.
Your might look something like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "run python script",
"type": "process",
"command": "python3 C:/Users/dickr/git/Poker/poker/main.py",
"options": {
"cwd": "C:/Users/dickr/git/poker"
},
"presentation": {
"reveal": "always",
}
}
]
}
though I'd suggest that if possible, you use variable substitution to try to get rid of the absolute paths that won't be the same on other machines (assuming you want to be able to run the task on other machines).
Then run the task. You can even bind the task to keyboard shortcuts. See the documentation for more info: https://code.visualstudio.com/docs/editor/tasks.

VS Code task to toggle the status bar on debug execution

I prefer to hide the status bar in VS code but one nice feature is that it changes colour when your program is ready to debug. This is good when there is a lengthy build step as part of the preLaunchTask. I would like to unhide and hide the taskbar using tasks.json. The internal command is
workbench.statusBar.visible true
But I am not sure how I can execute this in a task by vscode
{
"label": "debug with statusbar",
"type": "process", // ?
"group": "build",
"args": [ true ],
"command": "workbench.statusBar.visible",
"dependsOn":["npm: build"]
},
{
...
It appears a custom task can either execute via shell or as a process. Is there a way to call vscode via one of these methods and execute the internal command? Or is there another way to achieve what I would like?
Editing the settings file seems to do the trick since VS Code watches it in real time.
{
"label": "debug with statusbar",
"type": "shell",
"group": "build",
"args": [
"-i",
"'/\"workbench.statusBar.visible\": false/s/.*/ \"workbench.statusBar.visible\": true/'",
"/home/myuser/.config/Code/User/settings.json"
],
"command": "sed",
"dependsOn":["npm: build"]
},
Then an equivalent task can be used for postDebugTask

VS Code: Create file inside the current directory of the editor

Is it possible to create a new file inside the same folder as the file that is active in the editor by the time the keyboard shortcut ctrl+n for explorer.newFile is hit?
I can tell if it's the correct folder by looking at the breadcrumbs and mostly have the file explorer toggled off. The command, however, seems to create a file inside the last folder opened or focused by the file explorer.
You can do this by overloading that explorer.newFile command's default keybinding to run a task instead. Here is the keybinding:
{
"key": "ctrl+n",
"command": "workbench.action.tasks.runTask",
"args": "newFile",
"when": "editorFocus" // must have an editor focused
},
And the task (in tasks.json):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "newFile",
"command": "touch '${fileDirname}/${input:fileName}' && code '${fileDirname}/${input:fileName}'",
"type": "shell",
"problemMatcher": [],
},
],
"inputs": [
{
"type": "promptString",
"id": "fileName",
"description": "Complete my file name.",
"default": "new file name"
}
]
}
That will create a file in the same directory as the active editor's directory - it will ask you for the filename - and then open it.
I used the bash command touch to create the file (if it doesn't already exist), if you are using a different shell you will need the equivalent of touch.

Visual Studio Code Task is Prepending the working directory to the command

I'm trying to setup visual studio code for rust to do debugging. In my launch.json file I have
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/vscode-rust-debug-example.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "localcargobuildtask"
}
]
}
and in my tasks.json I have
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "localcargobuildtask",
"command": "echo hi",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Where "echo hi" is in my tasks I eventually want to have something like "cargo build" or "cargo test" or whatever. But this is just a starting step.
But when I press F5 the output I get is
> Executing task: C:\programs\VSCodeWorkspaces\RustProjects\vscode-rust-debug-example-debug-config-included\echo hi <
The terminal process terminated with exit code: 2
Terminal will be reused by tasks, press any key to close it.
Rather than running "echo" from where ever the terminal finds it (working directory first, then check global path variable like you would expect) it is actually looking for a program named "echo" in my root folder of the workspace.
How do I tell visual studio code "No I don't want you to run workspace/echo, I want you to run echo in workspace" ? Or is there another more direct way to tell visual studio code "compile this before you debug it" ?
The answer to this question How to debug Rust unit tests on Windows?
suggests that changing
"command": "cargo build",
into
"command": "cargo",
"args": [
"build"
],
in the tasks.json file works and somehow it does. Maybe the editor is configured to search the %path% for single word commands, or maybe some plugin I installed overwrote the "cargo" command. Maybe the reason echo wasn't found is because it is a terminal command, not a program. Maybe the error message was a lie and it was only reporting that it couldn't find workspacefolder\command despite checking %path%? Or maybe none of that. I have no idea. It is some pretty idiosyncratic behavior.

Can't get simple task to run in vscode

I am ultimately trying to set up vscode to build typescript, but I first just wanted to get a simple task to run, and I can't seem to get that to work. I now want to just run the "Hello world" task from https://code.visualstudio.com/docs/editor/tasks, which is to simply echo a string to the output window.
My tasks.json is in the .vscode folder, and its content is:
{
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": ["Hello World"],
"showOutput": "always"
}
When I try to run the task from the command palette and choosing "Tasks: Run Task," I see "no tasks found" when I expect to see this echo task. I don't know why I don't see this task in a task list.
What am I doing wrong?
FWIW, my vscode version is 1.11.1.
This works on current vscode:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "MyHelloTask",
"type": "shell",
"command": "echo",
"args": ["Hello\ World"],
"presentation": {
"echo": true,
"reveal": "always",
"panel": "shared"
}
}
]
}
What was wrong?
The property showOutput is deprecated. Use the reveal
property inside the presentation property instead.
See also the 1.14 release notes.
Also isShellCommand now became type, etc...
Also note the escaped space in the argument. (triggers explict complaint about it otherwise. Yes, despite the quotes around it.)