How do I automatically clear VS Code terminal when starting a build?
Asked Answered
E

9

65

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?

Elegy answered 14/9, 2017 at 14:5 Comment(8)
use new project instead of the new file to make a buildVondavonni
Visual Studio by default build the project file instead of your normal file. So make a new project. Please follow these steps here. #19684690Vondavonni
Visual Studio Code, not Visual Studio.Elegy
Sorry sir I thought it was Visual Studio , but for that you can use command lineVondavonni
I see two commands that I presume you could add to a build task:{ "workbench.debug.panel.action.clearReplAction" [clears the console] and "workbench.output.action.clearOutput" [clears the output].Lorri
@Lorri How exactly do you add VSCode commands like those to a task?Theogony
@Denis See https://mcmap.net/q/302490/-how-can-i-run-a-vscode-command-as-a-task for example.Lorri
Why is it so annoyingly complex to do that? I tried 5 answers before one workedDegradation
P
68

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.

Puente answered 6/11, 2018 at 21:13 Comment(3)
From the v1.30 release notes: code.visualstudio.com/updates/v1_30#_clear-task-terminalLorri
This also made it into the official documentation.Lattice
What I can't find in the documentation is the fact that you can add task properties like presentation outside any task to set the default for all tasks.Theogony
S
22

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.

Clear Previous Output from menu settings

Seller answered 3/2, 2019 at 23:18 Comment(2)
Not present in my VS Code; seems like you've got a "Code-runner" extension installed.Elegy
Code-runne extension must be installed for this settingCrib
B
14

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:

enter image description here

This will make sure that terminal remain clear on every run, thus ensuring only 1 file run is visible at a time.

Bohaty answered 27/5, 2021 at 1:2 Comment(6)
Hmmm I'm on a Mac running 1.57 and what your showing and I followed doesn't work. Are you running a separate extension ?Crux
I am doing this on windows laptop. You dont get this option "Clear" in your vs-code in mac?Bohaty
I do get the clear and check off the box. When running the code it runs once and if I run it again it's not clearing out the previous output. I came across the following. // Before starting a new debug session in an integrated or external terminal, clear the terminal. "debug.terminal.clearBeforeReusing": false,Crux
In my option, I was doing the step manually every time. So your option is better. Will test it.Bohaty
If you add the following it pretty much cleans up or presents the way people are asking about. import os os.system("clear")Crux
with this enabled, my python terminal still is not clearing when i click run button.Fitting
H
7

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.

Howler answered 14/9, 2017 at 14:28 Comment(5)
Not perfect, but better then the default.Monobasic
@Monobasic Why isn't it perfect? What's the difference (from a practical point of view) between opening a new terminal and clearing the terminal before the build?Sensate
@FabioTurati I guess it's a little frustrating that each new build creates a new task with this approach instead of just emptying the output of the existing task. It's definitely nicer than appending to the bottom of the existing output but not scrolling down to the newly generated output.Stoltzfus
@FabioTurati Indeed, now I have ever growing number of tasks in Terminal panel. I guess this is at least memory consuming. And distracting.Taliped
Note to future readers: the option to clear the terminal before each task run has now been accepted and merged (see this), but it may take some time for the option to appear in stable builds.Puente
H
7

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.

Housewarming answered 15/8, 2018 at 5:38 Comment(8)
"code-runner.clearPreviousOutput" - Unknown configuration setting.Taliped
I'm using VS Code 1.26.1. That line is definitely in my user settings. And it works.Housewarming
Ok. It looks like I installed the Runner extension. The setting belongs to the extension not the core. Seems to be a good extension.Housewarming
Good you checked it. This should be a part of the answer.Taliped
Have you installed the code runner extension? Code Runner does run you code "in" the terminal window, it runs a process that not place output tab. Once you have code runner installed you should be able to add the clearPreviousOutput settingHousewarming
I installed it, and added "code-runner.clearPreviousOutput": true option, and VS recognized it. Then I pressed Ctrl+Shift+B several times. The output was not cleared.Taliped
Not sure what is going on here. This was definitely working for me but with recent refresh code runner stopped working altogether so I removed it. I've started using the standard F5 run mechanism. This seems to be holding the buffer even after it is cleared. Looks like the whole thing needs some work.Housewarming
I've added the command in my settings.json and this works perfectly for me.Maines
P
6

Update Visual Code 1.54 +

To clean terminal when click Run.

  1. Install Code-runner extension.
  2. Setting > search "clear" -> Check on "Clear Previous Output" enter image description here enter image description here
Pineda answered 14/3, 2021 at 4:2 Comment(0)
P
4

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
            }
        }
    ]
}
Porbeagle answered 8/2, 2021 at 1:17 Comment(0)
J
1

If you control the build task yourself, it's easy to prepend a clear command:

"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "clear && make",
....
Joellajoelle answered 25/7, 2018 at 7:17 Comment(3)
Note that this does work nicely in linux, which helped me.Yorgo
I'm not sure it even works on linux anymore: /bin/bash: clear && g++: command not found // The terminal process terminated with exit code: 127Puente
This assumes the PATH contains a clear and a make command. I think it will work on these systems when launched from a shell where the commands do exist.Joellajoelle
K
0

This can also be done with a keybinding. Go to the command pallet and select "Preferences: Open Keyboard Shortcuts (JSON)". In my instance I'd like VSCode to clear the output, refresh the tests, and then run CTest through CMake (which will trigger a build if needed):

[
    {
        "key": "f5",
        "command": "runCommands",
        "args": {
            "commands": [
                "workbench.output.action.clearOutput",
                "testing.refreshTests",
                "cmake.ctest"
            ]
        }
    }
]

The list of available commands will vary depending on which plugins you have installed. A list of builtings can be found here: https://code.visualstudio.com/api/references/commands

Karyosome answered 2/8, 2023 at 14:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.