Here's code that works for both macOS' Terminal, and iTerm2. It doesn't need the window to be in the foreground (unlike some AppleScript solutions), either.
printf '\e[2J\e[3J\e[H'
How it works
This uses a series of ANSI escape sequences. Each ANSI escape sequence starts with the "ESC" (escape) character, it's a non-printable ASCII character (meaning it has no visual glyph representation like 1
or a
). printf
can print an this character using \e
(or by its octal value \033
, or its hex value \x1B
).
One kind of ANSI escape sequence are the "Control Sequence Introducer" commands. They all start with the form ESC CSI
, where ESC
is the escape character just mentioned, and CSI
is the value [
(or 0x5B
in hex)
I use the \e[
representation, because it's shorter than \x1B\x5B
, and easier to read.
As we can see, there are two ANSII escape sequences here, each of which are the "control sequence introducer" form, called with different arguments. Knowing this, we can split up the string into its 3 parts:
\e[2J
The start of an escape sequence (\e
), containing a control sequence ([
) with argument 2J
This is an instance of the "ED – Erase in Display" command, which has the form CSI n J
The n
value is set to 2
in this case, which invokes the second variant:
If n is 2
, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS).
\e[3J
The start of an escape sequence (\e
), containing a control sequence ([
) with argument 3J
This is an instance of the "ED – Erase in Display" command, which has the form CSI n J
The n
value is set to 3
in this case, which invokes the third variant:
If n is 3
, clear entire screen and delete all lines saved in the scrollback buffer (this feature was added for xterm and is supported by other terminal applications)."
Despite the description on Wikipedia, it appears that this isn't actually clearing the screen in iTerm and the default Terminal, only the scrollback buffer was cleared. Hence the need for the \e[2J
command above.
\e[H
:
The start of an escape sequence (\e
), containing a control sequence ([
) with argument H
.
This is an instance of the "CUP - Cursor Position" command, which has the form CSI n ; m H
,
Moves the cursor to row n, column m. The values are 1-based, and default to 1
(top left corner) if omitted. A sequence such as CSI ;5H
is a synonym for CSI 1;5H
as well as CSI 17;H
is the same as CSI 17H
and CSI 17;1H
This could have been more explicitly written as \e[1;1H
Fun fact
These control sequences are actually how the built-in clear
(/usr/bin/clear
) command works. All it seems to do is product some output, which we can inspect with xxd
:
$ clear | xxd
00000000: 1b5b 481b 5b32 4a .[H.[2J
We can re-create the same output with printf '\e[H \e2J'
:
$ printf '\e[H \e2J' | xxd
00000000: 1b5b 4820 1b32 4a .[H .2J
Notice they don't use \e3J
, hence why clear
only clears the screen, but not the scrollback.