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

65.5k views Asked by At

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?

9

There are 9 answers

5
shyammakwana.me On

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.

3
jedwards On

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.

6
Avneet On

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.

0
Mickael T On

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
            }
        }
    ]
}
3
xtofl On

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

"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "clear && make",
....
8
JimB2 On

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.

0
Tuan Huynh On

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
0
Richard Vodden On

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

2
Níkolas La Porta On

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