I'd recently expected the clear
-like command from a commandline PHP program to clear the screen and scrollback buffer, like Command-K does in iTerm. It didn't clear the scrollback, though. So I tried the ANSI or DEC vt100 escape sequence Esc[2J
. That didn't help either. The clearing wasn't important, though, searching for it was taking too much time, so I gave up on it. (Looking back, I probably could have found some iTerm documentation on the codes.)
I recently came across a solution for something else entirely, and it used the sequence Esc[3J
. It cleared the scrollback, too. I wanted to know which other parameters could be used in the Esc[ₓJ
sequence (which is called "erase in display" or "ED"), so I found these resources:
- https://en.wikipedia.org/wiki/ANSI_escape_code#ED
- http://apple.stackexchange.com/a/113168/20946
- http://www.vt100.net/docs/vt100-ug/chapter3.html#ED
- http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Functions-using-CSI-_-ordered-by-the-final-character_s_
As suggested on the Stack Exchange page, a good solution is to add an alias to ~/.bash_profile
:
alias clear="clear && printf '\e[3J'"