I had an issue. Using the clear or cls command in powershell clears only the visible portion of the terminal,I would like to know how to clear the entire terminal?
I use VSCode by the way.
tl;dr
The question is about clearing both the screen and the scrollback buffer in the integrated terminal of Visual Studio Code, and the next section addresses that.
To clear both the screen and the scrollback buffer in a regular console / terminal / in Windows Terminal:
Clear-Host
(whose aliases are cls
and on Windows only, also clear
; on Linux and macOS, clear
refers to the external /usr/bin/clear
utility, which has the same effect on Linux only).Write-Host -NoNewLine "`e[2J`e[3J`e[H"
from PowerShell, or printf '\033[2J\033[3J\033[H'
from POSIX-compatible shells; in PowerShell 7.4+ on macOS 14+ (Sonoma or higher), [Console]::Clear()
works now too.To also clear the scrollback buffer, not just the visible portion of the terminal in Visual Studio Code's integrated terminal, use one of the following methods:
Caveat: The following methods do not play nicely with PowerShell (both editions, up to at least v7.4.1; other shells seem to be fine): Even though clearing works fine, PowerShell is then confused about the cursor position when the next command is typed - this problem has been reported in GitHub issue #19479.
Use the command palette:
tclear
to match the Terminal: Clear
command and press EnterUse the integrated terminal's context menu:
Clear
from the context menu.terminal.integrated.rightClickBehavior
to either default
or selectWord
(the latter selects the word under the cursor before showing the context menu).Use a keyboard shortcut from inside the integrated terminal (current as of v1.71 of VSCode):
On macOS, a shortcut exists by default: Cmd+K
On Linux and Windows, you can define an analogous custom key binding, Ctrl+K, as follows, by directly editing file keybindings.json
(command Preferences: Open Keyboard Shortcuts (JSON)
from the command palette), and placing the following object inside the existing array ([ ... ]
):
{
"key": "ctrl+k",
"command": "workbench.action.terminal.clear",
"when": "terminalFocus && terminalHasBeenCreated || terminalFocus && terminalProcessSupported"
}
Using a command you can invoke from a shell in the integrated terminal:
Note: A truly cross-platform solution would require executing the VSCode-internal workbench.action.terminal.clear
command directly from a shell, but I don't know how to do that / if it is possible at all - do tell us if you know.
Linux (at least as observed on Ubuntu):
Use the standard clear
utility (/usr/bin/clear
), which also clears the scrollback buffer.
From PowerShell, you may also use Clear-Host
(which calls /usr/bin/clear
) or its built-in alias, cls
.
[Console]::Clear()
clears the scrollback buffer too, but only in PowerShell 7.4+ (.NET 8+).
macOS:
As of at least macOS Sonoma (14+) /usr/bin/clear
now does appear to clear the scrollback buffer too in VS Code (though, curiously, still not in regular terminals), and therefore also PowerShell's Clear-Host
, which calls it behind the scenes.
On macOS Sonoma (14+) and PowerShell 7.4+ / .NET 8 [Console]::Clear()
now works too.
Alternatively, on older versions, print the following ANSI control sequence: '\e[2J\e[3J\e[H'
(\e
represents the ESC char. (0x1b
, 27
); e.g., from bash
: printf '\e[2J\e[3J\e[H'
; from PowerShell: Write-Host -NoNewLine "`e[2J`e[3J`e[H"
You can easily wrap this call in a shell script for use from any shell: create a file named, say, cclear
, in a directory listed in your system's PATH
variable, then make it executable with chmod a+x
; then save the following content to it:
#!/bin/bash
# Clears the terminal screen *and the scrollback buffer*.
# (Needed only on macOS, where /usr/bin/clear doesn't do the latter.)
printf '\e[2J\e[3J\e[H'
Windows:
As of Visual Studio Code v1.86.1, the standard commands - cls
in cmd.exe
and Clear-Host
/ cls
in PowerShell - do also clear the scrollback buffer (they didn't use to), but doing so comes with bugs in the integrated terminal:
In cmd.exe
, and in PowerShell (Core) (observed in v7.4.1) when run inside the PIC (the PowerShell Integrated Console, a special-purpose shell for developing PowerShell code via the PowerShell extension), one line is unexpectedly retained in the scrollback buffer.
Additionally, in PowerShell (Core) (observed in v7.4.1), also when running as a regular shell, the shell-integration decorations aren't reliably cleared (the marks to the left and right (scrollbar) of the terminal).
By contrast, Windows PowerShell (at least when running as a stand-alone shell) does not seem to have these problems.
As a workaround for the current bugs, you can override Clear-Host
as follows (using the same ANSI/VT escape sequences that work on macOS and Linux too; place it in your $PROFILE
file):
function Clear-Host {
Write-Host -NoNewLine "$([char]27)[2J$([char]27)[3J$([char]27)[H"
}