Home > Software engineering >  How to invoke cmd.exe /c cls from within VS Code tasks.json?
How to invoke cmd.exe /c cls from within VS Code tasks.json?

Time:10-06

I have a simple HelloWorld.cpp file and I want to run it with the following steps, each is run one after the other as follows.

  • Compile
  • Clear Integrated Terminal
  • Run the produced executable.

Unfortunately I fails to setup the second step (clearing the console window). What is the correct setup?

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "COMPILE",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${fileBasenameNoExtension}.exe",
                "${file}",
                "/std:c  latest"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ]
        },
        {
            "label": "CLEAN",
            "command": "cmd.exe /c cls",
            "dependsOn": "COMPILE"
        },
        {
            "label": "RUN",
            "command": "${fileBasenameNoExtension}.exe",
            "dependsOn": "CLEAN",
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

CodePudding user response:

there is a vscode setting with workbench.action.terminal.clear you'll find related settings there, or in File >Preferences >Keyboard shortcuts. search for Terminal:clear and assign the key

CodePudding user response:

Aha. I found the solution: Add "type": "shell" to the CLEAN step as follows.

{
    "type": "shell",
    "label": "CLEAN",
    "command": "cls",
    "dependsOn": "COMPILE"
}

The complete tasks.json.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "COMPILE",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${fileBasenameNoExtension}.exe",
                "${file}",
                "/std:c  latest"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ]
        },
        {
            "type": "shell",
            "label": "CLEAN",
            "command": "cls",
            "dependsOn": "COMPILE"
        },
        {
            "label": "RUN",
            "command": "${fileBasenameNoExtension}.exe",
            "dependsOn": "CLEAN",
            "options": {
                "cwd": "${fileDirname}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
  • Related