55

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?

Gama11
  • 31,714
  • 9
  • 78
  • 100
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380

9 Answers9

63

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.

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • 1
    From the v1.30 release notes: https://code.visualstudio.com/updates/v1_30#_clear-task-terminal – Mark Dec 13 '18 at 04:47
  • This also made it into the [official documentation](https://code.visualstudio.com/docs/editor/tasks#_output-behavior). – IInspectable May 20 '20 at 06:05
  • 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. – Denis Howe May 21 '21 at 15:24
19

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

9

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.

Avneet
  • 393
  • 4
  • 15
  • 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 ? – Michael1775 Jun 30 '21 at 11:32
  • I am doing this on windows laptop. You dont get this option "Clear" in your vs-code in mac? – Avneet Jun 30 '21 at 23:02
  • 1
    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, – Michael1775 Jul 01 '21 at 01:17
  • In my option, I was doing the step manually every time. So your option is better. Will test it. – Avneet Jul 01 '21 at 17:02
  • If you add the following it pretty much cleans up or presents the way people are asking about. import os os.system("clear") – Michael1775 Jul 06 '21 at 21:27
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.

shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50
  • Not perfect, but better then the default. – clankill3r Oct 17 '17 at 21:15
  • @clankill3r 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? – Fabio says Reinstate Monica Feb 13 '18 at 16:31
  • @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. – SDJMcHattie Apr 11 '18 at 16:23
  • 1
    @FabioTurati Indeed, now I have ever growing number of tasks in Terminal panel. I guess this is at least memory consuming. And distracting. – Mikhail Aug 23 '18 at 08:32
  • 8
    **Note to future readers:** the option to clear the terminal before each task run has now been accepted and merged (see [this](https://stackoverflow.com/a/53180094/736937)), but it may take some time for the option to appear in stable builds. – jedwards Nov 06 '18 at 21:16
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.

jim birch
  • 413
  • 4
  • 8
  • "code-runner.clearPreviousOutput" - Unknown configuration setting. – Mikhail Aug 23 '18 at 08:30
  • 1
    I'm using VS Code 1.26.1. That line is definitely in my user settings. And it works. – jim birch Aug 28 '18 at 03:52
  • Ok. It looks like I installed the Runner extension. The setting belongs to the extension not the core. Seems to be a good extension. – jim birch Aug 28 '18 at 12:02
  • Good you checked it. This should be a part of the answer. – Mikhail Aug 28 '18 at 12:18
  • 1
    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 setting – jim birch Sep 13 '18 at 23:56
  • 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. – Mikhail Sep 14 '18 at 20:01
  • 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. – jim birch Oct 03 '18 at 11:09
  • I've added the command in my settings.json and this works perfectly for me. – Saurabh Rana Dec 23 '18 at 19:29
4

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
Tuan Huynh
  • 566
  • 4
  • 13
1

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

"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "clear && make",
....
xtofl
  • 40,723
  • 12
  • 105
  • 192
  • Note that this does work nicely in linux, which helped me. – moodboom Sep 14 '18 at 16:56
  • I'm not sure it even works on linux anymore: `/bin/bash: clear && g++: command not found` // `The terminal process terminated with exit code: 127` – jedwards Nov 06 '18 at 20:34
  • 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. – xtofl May 10 '21 at 06:51
1

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
            }
        }
    ]
}
Mickael T
  • 895
  • 2
  • 8
  • 19
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

Richard Vodden
  • 318
  • 3
  • 12