19

Why bother?

Clearing scrollback buffer is handy in many ways, for example, when I wish to run some command with long output, and want to quickly scroll to start of this output. When scrollback buffer is cleared, I can just scroll to top, and will be done.

Some considerations:

There is clear command, according to man,

clear clears your screen if this is possible, including its scrollback buffer (if the extended "E3" capability is defined).

In gnome-terminal clear does not clear scrollback buffer. (What is "E3" capability, though?)

There is also reset, which clears, but it does a little bit more than that, and it is really slow (on my system it takes more than a second, which is significant delay for humans to be noticed).

And there is echo -ne '\ec' or echo -ne '\033c', which does the job. And indeed it is much faster than reset.

The question is, what is \ec sequence, how it differs from what clear and reset does, and why there is no separate command for it?

There is also readline's C-l key sequence, which by default bound to clear-screen command (I mean, readline command, not shell command). What is this command? Which escape sequence it emits? How does it actually work? Does it run shell command? Or what? Again, in gnome-terminal, it seems like it works just by spiting out blank lines until prompt appear in top line of terminal. Not sure about other terminal emulators. This is very cumbersome behavior. It pollutes scrollback with chunks of emptiness, so you must scroll up more, and more. It is like a hack, rather than clean solution.

Another question is, is there a readline command for mentioned \ec sequence? I want to bound it to C-l instead because I always want to clear scrollback buffer when I clear the screen.

And another question is how to just type such escape sequence into terminal, to perform desired action? Then do not have to think about binding C-l to another readline command (if such command exists). I tried typing Esc, then c but this does not work.

UPDATE This question answered mostly here: https://unix.stackexchange.com/a/375784/257159. It is very good answer which explains almost all questions asked here.

2

4 Answers 4

11

From the man bash's readline section:

   clear-display (M-C-l)
          Clear  the  screen  and,  if possible, the terminal's scrollback buffer, then redraw the current line, leaving the
          current line at the top of the screen.
   clear-screen (C-l)
          Clear the screen, then redraw the current line, leaving the current line at the top of the screen.  With an  argu‐
          ment, refresh the current line without clearing the screen.

so press control + alt + L

2
  • 2
    Available since bash 5.1: tiswww.case.edu/php/chet/bash/NEWS, see 2. New Features in Readline, point e. Very nice addition, though.
    – Anthony
    Aug 24, 2021 at 14:14
  • press control + alt + L...and be promptly locked out of the current session
    – smac89
    Nov 4, 2023 at 3:25
5

Many terminal emulators use the same escape sequences as XTerm, or similar. For XTerm, Escc is defined as "Full Reset (RIS)". It seems reasonable that a full reset would clear the scrollback buffer.


I have had no success using readline to bind to the sequence itself, but a workaround is

bind '"\C-l": "\C-a\C-kprintf\ "\\033c"\C-m"'

This has some caveats. First, it puts the printf command in your shell history, which is not ideal. Second, it cannot be used while inputting a command; it overwrites the current input line.

4

This works for me:

printf '\033[3J'

(from clear man page)

0

part of "https://invisible-island.net/ncurses/terminfo.ti.html#tic-xterm-basic" (Note that "\E stands for an escape (\033) character," and E3=\E[3J )

#### VT100/ANSI/ECMA-48
#
# ANSI Standard (X3.64) Control Sequences for Video Terminals and Peripherals
# and ECMA-48 Control Functions for Coded Character Sets.
#
# Much of the content of this comment is adapted from a table prepared by
# Richard Shuford, based on a 1984 Byte article.  Terminfo correspondences,
# discussion of some terminfo-related issues, and updates to capture ECMA-48
# have been added.  Control functions described in ECMA-48 only are tagged
# with * after their names.
#
# The table is a complete list of the defined ANSI X3.64/ECMA-48 control
# sequences.  In the main table, \E stands for an escape (\033) character,
# SPC for space.  Pn stands for a single numeric parameter to be inserted
# in decimal ASCII.  Ps stands for a list of such parameters separated by
# semicolons.  Parameter meanings for most parameterized sequences are
# described in the notes.
#
# Sequence     Sequence                             Parameter   or
# Mnemonic     Name              Sequence           Value      Mode   terminfo
# -----------------------------------------------------------------------------
# APC  Applicatn Program Command \E _                -         Delim  -
# BEL  Bell *                    ^G                  -         -      bel
# BPH  Break Permitted Here *    \E B                -         *      -
# BS   BackSpace *               ^H                  -         EF     -
# CAN  Cancel *                  ^X                  -         -      -   (A)
# CBT  Cursor Backward Tab       \E [ Pn Z           1         eF     cbt
# CCH  Cancel Previous Character \E T                -         -      -
# CHA  Cursor Horizntal Absolute \E [ Pn G           1         eF     hpa (B)
# CHT  Cursor Horizontal Tab     \E [ Pn I           1         eF     tab (C)
# CMD  Coding Method Delimiter * \E
# CNL  Cursor Next Line          \E [ Pn E           1         eF     nel (D)
# CPL  Cursor Preceding Line     \E [ Pn F           1         eF     -
# CPR  Cursor Position Report    \E [ Pn ; Pn R      1, 1      -      -   (E)
# CSI  Control Sequence Intro    \E [                -         Intro  -
# CTC  Cursor Tabulation Control \E [ Ps W           0         eF     -   (F)
# CUB  Cursor Backward           \E [ Pn D           1         eF     cub
# CUD  Cursor Down               \E [ Pn B           1         eF     cud
# CUF  Cursor Forward            \E [ Pn C           1         eF     cuf
# CUP  Cursor Position           \E [ Pn ; Pn H      1, 1      eF     cup (G)
# CUU  Cursor Up                 \E [ Pn A           1         eF     cuu
[1]: https://i.stack.imgur.com/IE2gD.png

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .