How do I automatically clear VS Code terminal when starting a build? - visual-studio-code

I press Ctrl+Shift+B to start a build in Visual Studio Code (it's configured to just run GNU Make), and the build tool output is written to the Terminal window.
However, it's appended to the output from the previous build, which is confusing.
How do I configure VS Code to clear the terminal window before starting a new build?

November 2018 Update
As of this commit (and a few subsequent follow-ups), you can now add a clear presentation option to your task to have it clear the terminal before each task run.
Working example (on fresh clone+build):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "[gcc] Build",
"type": "shell",
"command": "g++",
"args": [
"source.h",
"-Wall",
"-o",
"a.out"
],
"presentation": {
"clear": true // <-- this line
}
}
]
}
(Note: the linked commit diff has the key being named clearBeforeExecuting but it's apparently since been changed to just clear).
Prior to this, I created a clear_g++ script on my path with just:
#!/bin/bash
clear
exec g++ $*
And changed my command from g++ to clear_g++.
Since I liked the idea of this approach but it didn't end up working out.

You can change from settings menu (at least from version 1.30.2 and above)...
On Mac, just hit Code > Preferences > Settings.
Then just search for "clear" and check Clear Previous Output.

I tried to find a solution but can't. Simple hack I tried is to open new build in new tab. Add this presentation key to your task in tasks.json
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "new"
}
panel:new will open in new terminal.

Add this user setting to clear the OUTPUT tab on clicking run (▶)
"code-runner.clearPreviousOutput": true,
This is not the same as clearing the terminal but it might be what someone wants.
[Edit] This requires the Runner extension, which I'd recommend for testing/running scripts directly within VS Code.

New Visual Studio code 1.56. This works in windows.
You simply go to Preferences:Open Settings(UI), search "Clear" and check option as below:
This will make sure that terminal remain clear on every run, thus ensuring only 1 file run is visible at a time.

Update Visual Code 1.54 +
To clean terminal when click Run.
Install Code-runner extension.
Setting > search "clear" -> Check on "Clear Previous Output"

If you control the build task yourself, it's easy to prepend a clear command:
"tasks": [
{
"label": "build",
"type": "shell",
"command": "clear && make",
....

In Visual Studio Code version 1.52.1, the clearing by default of the terminal is achieved with the clear: true property (=Controls whether the terminal is cleared before executing the task.). Unfortunately it does not do the job, I still see the terminal with older messages. I have to manually enter "clear" in the terminal to clear it completely.
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
This is added in tasks.json which looks like this under OSX:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++11",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/clang++",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
}
]
}

Related

VSCode terminal task not using zsh profile

I'm trying to run a task on window load in VSCode where a terminal opens and nvm use && yarn dev is run by default. However, running this shell tasks seems to not load my zsh profile.
The output I get from running my task is:
The terminal process "zsh '-c', 'nvm use && yarn dev'" terminated with exit code: 127.
Terminal will be reused by tasks, press any key to close it.
But if I then manually start a new terminal and run the same command (ie: by pressing plus, opening a new integrated terminal), it will work as intended.
Suspecting that VSCode isn't loading my profile for some reason, I tried adding the following to my task, it resulted in the error /bin/zsh: can't open input file: nvm use && yarn dev The terminal process "zsh '-l', 'nvm use && yarn dev'" terminated with exit code: 127..
// in dev task
"options": {
"shell": {
"executable": "zsh",
"args": ["-l"]
}
},
.vscode/tasks.json
{
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "dedicated",
"showReuseMessage": true
},
"tasks": [
{
"label": "Create terminals",
"dependsOn": [
"Dev",
],
// Mark as the default build task so cmd/ctrl+shift+b will create them
"group": {
"kind": "build",
"isDefault": true
},
// Try start the task on folder open
"runOptions": {
"runOn": "folderOpen"
}
},
{
"label": "Dev",
"type": "shell",
"command":
["nvm use && yarn dev"],
"isBackground": true,
"problemMatcher": [],
"presentation": {
"group": "dev-group"
}
},
]
}
This worked for me-
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh",
"args": ["-l", "-i"]
}
},
github.com/microsoft/vscode/issues/143061
try adding this to your settings.json
"terminal.integrated.profiles.osx": {
[...]
"zsh": {
"path": "/bin/zsh -l",
"args": [
"-l"
]
},
[...]
},
Note that the important part is
"path": "/bin/zsh -l",
I had the same problem and I found that for some reason VScode does not take into consideration the -l flag passed in args. So you can just include it with path.
If you do not have terminal.integrated.profiles.osx in your settings, you can copy it from the default settings (open the Command Palette and search for 'default settings').
I did not need to do this, but you can make sure that zsh is the default terminal profile for VScode by setting terminal.integrated.defaultProfile.osx to zsh
Try running echo $SHELL from VSCode's integrated terminal. If you're on a Mac or Linux machine, you can compare that output to the output from the terminal app (outside VSCode). It's possible your default shell in VSCode is set incorrectly or using a copy of zsh at another location. If so, set VSCode's default shell through the command palette (Terminal: Select Default Shell).
Also check out your shell's default profile (Terminal: Select Default Profile) from the command palette and make sure it's set to zsh -l... using the -c argument (non-login non-interactive) will prevent ~/.zshrc from being executed, which sounds like what's going on here given your error output.
Finally, confirm your profile is located correctly (at ~/.zshrc) and that both nvm and yarn PATHs are exported. Alternatively, if you're trying to reference yarn locally (if for some reason you only installed it locally), you'll need to run yarn via npx...
You may need to add an automation profile as well
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh -l",
"args": ["-l"]
}
},
"terminal.integrated.automationProfile.osx": {
"path": "/bin/zsh"
}
macOS 12.6.1 | vscode 1.74.0
I did not manage to do any if it since none of this worked, so I have just removed warning...
"terminal.integrated.showExitAlert": false
Or via GUI
I hope that will not get minus points here...

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.

Running multiple projects but separate output in Visual Code

Visual Code provides the concept of compounds configuration to launch multiple processes. However, all this processes log into one single Debug Console. This is quite ugly. Is there any possibility to have one output windows/tab/something for each process that is running?
If you are using task configuration (tasks.json) you can set the panel attribute inside presentation. It controls if the panel is shared between tasks, dedicated to this task or a new one is created on every run:
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
}
]
}
By default is shared but you can have dedicated or new

Visual Studio Code 1.17.1 Open in Browser without opening terminal

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

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.)