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

38,617

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
38,617

Related videos on Youtube

Roger Lipscombe
Author by

Roger Lipscombe

I was a Windows programmer (15 years of C++, C#, SQL Server), but I took a leap of faith and now I'm a backend software developer, using Erlang and Elixir, at Twilio.

Updated on May 27, 2021

Comments

  • Roger Lipscombe
    Roger Lipscombe almost 3 years

    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?

    • babygame0ver
      babygame0ver over 6 years
      use new project instead of the new file to make a build
    • babygame0ver
      babygame0ver over 6 years
      Visual Studio by default build the project file instead of your normal file. So make a new project. Please follow these steps here. stackoverflow.com/questions/19684190/…
    • Roger Lipscombe
      Roger Lipscombe over 6 years
      Visual Studio Code, not Visual Studio.
    • babygame0ver
      babygame0ver over 6 years
      Sorry sir I thought it was Visual Studio , but for that you can use command line
    • Mark
      Mark over 6 years
      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].
    • Denis Howe
      Denis Howe about 3 years
      @Mark How exactly do you add VSCode commands like those to a task?
    • Mark
      Mark about 3 years
      @Denis See stackoverflow.com/a/57470981/836330 for example.
  • clankill3r
    clankill3r over 6 years
    Not perfect, but better then the default.
  • Fabio says Reinstate Monica
    Fabio says Reinstate Monica about 6 years
    @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?
  • SDJMcHattie
    SDJMcHattie about 6 years
    @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.
  • Mikhail
    Mikhail over 5 years
    "code-runner.clearPreviousOutput" - Unknown configuration setting.
  • Mikhail
    Mikhail over 5 years
    @FabioTurati Indeed, now I have ever growing number of tasks in Terminal panel. I guess this is at least memory consuming. And distracting.
  • jim birch
    jim birch over 5 years
    I'm using VS Code 1.26.1. That line is definitely in my user settings. And it works.
  • jim birch
    jim birch over 5 years
    Ok. It looks like I installed the Runner extension. The setting belongs to the extension not the core. Seems to be a good extension.
  • Mikhail
    Mikhail over 5 years
    Good you checked it. This should be a part of the answer.
  • jim birch
    jim birch over 5 years
    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
  • moodboom
    moodboom over 5 years
    Note that this does work nicely in linux, which helped me.
  • Mikhail
    Mikhail over 5 years
    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.
  • jim birch
    jim birch over 5 years
    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.
  • jedwards
    jedwards over 5 years
    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
    jedwards over 5 years
    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.
  • Mark
    Mark over 5 years
  • Saurabh Rana
    Saurabh Rana over 5 years
    I've added the command in my settings.json and this works perfectly for me.
  • Roger Lipscombe
    Roger Lipscombe about 5 years
    Not present in my VS Code; seems like you've got a "Code-runner" extension installed.
  • IInspectable
    IInspectable almost 4 years
    This also made it into the official documentation.
  • xtofl
    xtofl almost 3 years
    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.
  • Denis Howe
    Denis Howe almost 3 years
    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.
  • Michael1775
    Michael1775 almost 3 years
    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 ?
  • Avneet
    Avneet almost 3 years
    I am doing this on windows laptop. You dont get this option "Clear" in your vs-code in mac?
  • Michael1775
    Michael1775 almost 3 years
    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,
  • Avneet
    Avneet almost 3 years
    In my option, I was doing the step manually every time. So your option is better. Will test it.
  • Michael1775
    Michael1775 almost 3 years
    If you add the following it pretty much cleans up or presents the way people are asking about. import os os.system("clear")