Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the entire terminal (PowerShell)

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.

like image 601
Malik Bagwala Avatar asked Sep 01 '18 11:09

Malik Bagwala


People also ask

How do I clear code in terminal?

To clear Terminal in VS Code simply press Ctrl + Shift + P key together this will open a command palette and type command Terminal: Clear . Also you will go to View in taskbar upper left corner of vs code and open Command pallete.

What is cls in PowerShell?

The Clear-Host function removes all text from the current display, including commands and output that might have accumulated. When complete, it displays the command prompt. You can use the function name or its alias, cls .

How do I clear the terminal in Windows?

From the Windows command line or MS-DOS, you can clear the screen and all commands using the CLS command.


1 Answers

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 keybord shortcut from inside the integrated terminal:

    • On Windows, use Ctrl+K, the default.
    • As of VSCode 1.30.1, on macOS, a default exists, Cmd+K, but doesn't work, whereas on Linux there is no default at all.
      The solution in both cases is to define a custom key binding as follows, by directly editing file keybindings.json (command Preferences: Open Keyboard Shortcuts File from the command palette):
{
  "key": "ctrl+k", // on macOS, alternatively use "cmd+k"
  "command": "workbench.action.terminal.clear",
  "when": "terminalFocus" // To avoid conflicts with Ctrl+K *chords* elsewhere
}

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.
  • macOS:

    • Print the following ANSI control sequence (unfortunately, the standard /usr/bin/clear utility clears only one screenful, not also the scrollback buffer): '\e[2J\e[3J\e[H' (\e represents the ESC char. (0x1b, 27); e.g., from bash: printf '\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).
like image 118
mklement0 Avatar answered Sep 16 '22 19:09

mklement0