6

I'd like to clear the scrollback buffer on Linux console VTs programmatically. i.e. not just clear the current visible screen, but the entire scrollback buffer, too. I.e. everything that after a clear screen would still be visible with Alt-PgUp should be gone too. Anybody got an idea how to achieve that in nice code?

braX
  • 11,506
  • 5
  • 20
  • 33
user175104
  • 3,598
  • 2
  • 23
  • 20

3 Answers3

6

I don't think this is in mainline yet, but linux-next has a patch to support a new console escape sequence that clears the screen and the scrollback buffer: CSI 3 J

For something that works without having to upgrade your kernel, you can use:

chvt 42; chvt <current tty no>; echo -en "\e[1;1H\e[2J"

Alternatively:

echo -e "\e[12;42]"; sleep .01; echo -en "\e[12;<current tty no>]\e[1;1H\e[2J"

You can get the current tty number with:

$( ls -l /proc/self/fd | sed -ne 's/.*tty//p' )
ninjalj
  • 42,493
  • 9
  • 106
  • 148
1

Keep in mind that other tools (over ssh for example) will have differing implementations of the "scrollback buffer". I highly doubt that you can clean the putty buffer by code in your machine. Also see https://superuser.com/questions/122911/bash-reset-and-clear-commands

Community
  • 1
  • 1
Mr47
  • 2,655
  • 1
  • 19
  • 25
-1

This clears the screen, but not the scrollback.

echo -e '\0033\0143' #depends on the terminal emulator you are using, tested on gnome terminal

from https://superuser.com/questions/122911/bash-reset-and-clear-commands

Community
  • 1
  • 1
ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52