How to Clear the Scrollback in the Screen Command

How to clear the scrollback in the screen command?

This thread has the following suggestion:

In the window whose scrollback you want to delete, set the scrollback to zero, then
return it to its normal value (in your
case, 15000).

If you want, you can bind this to a
key:

bind / eval "scrollback 0" "scrollback 15000"

You can issue the scrollback 0 command from the session as well, after typing C-a :.
HTH.

GNU screen: how to clear scrollback and screen at once

Turns out you can just do:

  bind _ eval "clear" "scrollback 0" "scrollback 15000"

Note that the order seems to matter: it doesn't seem to work every time if you have the "clear" at the end (it always clears the screen, but not always the scrollback).

How do I clear the scrollback buffer in tmux?

This same question has been plaguing me for quite some time. Here's the best I've come up with. Put this into your .tmux.conf file:

bind -n C-k clear-history

This binds ctrl-k to the tmux clear-history command. The -n after bind makes it so you don't have to issue the tmux command prefix (ctrl-b by default). I use bash, so ctrl-l already does the equivalent of typing "clear" at the command line. With these two keys I get a nice ctrl-l, ctrl-k combo, which moves all the scroll buffer off the screen (the "clear") and then deletes all that history (the tmux "clear-history" command).

It's not quite as nice as Terminal's, iTerm's, or Konsole's 1-key combos for clearing it out, but it's a world better than typing in clear-history all the time.

How to clear the entire terminal (PowerShell)

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.)

Ways to Clear the Console?

There is no reliable way that works everywhere. You've already mostly discovered this.

Generally, command-line output goes into a terminal scrollback buffer, of which only the last n lines are displayed, but previous lines are available through a scrolling mechanism. A command-line program does not write directly to this buffer, but writes to stdout which is, in most cases, piped to the terminal process which then displays the data.

Some terminal programs (i.e. those supporting ANSI escapes) might let you clear the visible portion of the screen. As far as I know, only Windows' cmd.exe responds to a 'clear screen' request by clearing the entire scrollback buffer. On Linux AFAIK it's not possible to discard the buffer completely. And, on Windows, cls is not an executable command but a shell builtin, so you cannot run it from Java System.exec().

Also, any command-line program can have its output redirected to a file with out the program being aware of it, in which case 'clear screen' doesn't have much meaning.

If you MUST have this level of control then you will have to write your own display window using Swing, AWT or GWT or whatever and manage all interaction there.

Clear the screen and buffer

If you using Windows you can use this

#include <stdio.h>

int main(void)
{
system("cls"); //call to system function to clear screen.
return 0;
}

This thread has the following suggestion:

In the window whose scrollback you want to delete, set the scrollback to zero, then return it to its normal value (in your case, 15000).

If you want, you can bind this to a key:

bind / eval "scrollback 0" "scrollback 15000"

You can issue the scrollback 0 command from the session as well, after typing C-a :. HTH.



Related Topics



Leave a reply



Submit