Skip to content

Instantly share code, notes, and snippets.

@sloanlance
Created February 8, 2017 16:08
Show Gist options
  • Save sloanlance/507951e4558a07411ec02365e096b811 to your computer and use it in GitHub Desktop.
Save sloanlance/507951e4558a07411ec02365e096b811 to your computer and use it in GitHub Desktop.
Clear terminal and scrollback

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:

As suggested on the Stack Exchange page, a good solution is to add an alias to ~/.bash_profile:

alias clear="clear && printf '\e[3J'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment