29

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.

Malik Bagwala
  • 2,695
  • 7
  • 23
  • 38

2 Answers2

62

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:

    • On Windows and Linux: Use 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).
    • On macOS: Execute "`e[2J`e[3J`e[H" (or printf '\033[2J\033[3J\033[H').

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:

  • Use the command palette:

    • Press Ctrl+Shift+P and type tclear to match the Terminal: Clear command and press Enter
  • Use the integrated terminal's context menu:

    • Right-click in the terminal and select Clear from the context menu.
    • On Windows, you may have to enable the integrated terminal's context menu first, given that by default right-clicking pastes text from the clipboard:
      Open the settings (Ctrl+,) and change setting 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 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 or its built-in alias, cls.

      • By contrast, [Console]::Clear() does NOT clear the scrollback buffer and clear just one screenful.
  • macOS:

    • Unfortunately, neither /usr/bin/clear nor PowerShell's Clear-Host (cls) nor .NET's [Console]::Clear() clear the scrollback buffer - they all clear just one screenful.

    • 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: "`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:

    • NO solution that I'm aware of: cmd.exe's internal cls command and PowerShell's internal Clear-Host command clear only one screenful in the integrated terminal (not also the scrollback buffer - even though they also do the latter in a regular console window and in Windows Terminal).

    • Unfortunately, the escape sequence that works on macOS ("`e[2J`e[3J`e[H" or, for Windows PowerShell, "$([char]27)[2J$([char]27)[3J$([char]27)[H") is not effective: on Windows it just clears one screenful.

    • (By contrast, all of these methods do also clear the scrollback buffer in regular console windows and Windows Terminal.)

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • In ubuntu we can easily do this at the command line: 'clear; jest' to wipe the scroll buffer and run a test. Is there a _command line_ option we can use that does the same inside of vscode for either command shell or powershell? – alfreema Jan 20 '22 at 16:23
  • 1
    @alfreema, please see my update. – mklement0 Jan 20 '22 at 20:12
  • 1
    Bummer, we have aliases that work great in Ubuntu but haven't discovered a way to do the same in Windows because we can't clear that pesky scrollback buffer with a command. – alfreema Jan 26 '22 at 14:11
  • Unfortunate, indeed, @alfreema - I suggest opening an issue at https://github.com/microsoft/vscode/issues – mklement0 Jan 26 '22 at 14:16
0

right click on the powershell button, then select clear, when you are at the command window, type "clear" command, to clear the terminal window.