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

我与影子孤独终老i 提交于 2020-06-07 13:41:07

问题


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?


回答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.




回答2:


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:


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.




回答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.




回答5:


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

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


来源:https://stackoverflow.com/questions/46221272/how-do-i-automatically-clear-vs-code-terminal-when-starting-a-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!