When using Terminal.app, you can clear the screen by using the shell builtin clear or by pressing ^+L (Control-L).

However, all this does is push the current screen content back one screen height and reset the cursor/input at the first line. Meaning you can still scroll back and see it.

What you are also able to do, is reset your entire scrollback by pressing +K (Command-K).

After you've done this, you cannot scroll back at all.

In certain situations (notably, before running screen or vim), I'd like to reset the scroll back before the command actually executes.

Is there a command (like clear) that is implemented in OS X that allows me to do this? Given the existence of pbcopy and pbpaste, I'm thinking something similar might exist that will allow me to do this.

  • As Chris Pages answer says in Terminal use command: clear && printf '\e[3J' (You have selected the wrong answer as correct answer!) – Cyborg Aug 23 '17 at 18:39

Terminal supports an extension of the ED (Erase in Display) escape sequence to erase the scroll-back. It is also supported by xterm. The ED command, described in the VT100 manual, accepts these values for the Ps parameter:

ESC [ Ps J

Parameter   Parameter Meaning

0           Erase from the active position to the end of the screen
1           Erase from start of the screen to the active position
2           Erase all of the display

Terminal (and xterm) adds:

3           Erase the scroll-back (aka “Saved Lines”)

Note that this only erases the scroll-back, not the screen. This allows you to erase one or the other, or both by sending two escape sequences.

For example, you can clear the screen and the scroll-back with the following shell command: clear && printf '\e[3J'

(The clear command looks up the appropriate sequence for clearing the screen for the current terminal, but the “erase scroll-back” escape sequence is custom and must be hard-coded. If you put this in a shell script that you don’t know for certain will only ever be run with Terminal, you should check that $TERM_APPLICATION is Apple_Terminal before sending it.)

  • 4
    This is so much faster than osascript! – Sergei May 28 '14 at 13:35
  • 3
    This is a much better answer. So, to make this permanent, add to your ~/.bash_profile: alias clear="clear && printf '\e[3J'" – Tom Jun 15 '15 at 14:59
  • 1
    I put this script in my PATH: echo $'#!/usr/bin/env bash\n/usr/bin/clear\nprintf \'\\e[3J\'' >clr; chmod +x clr – Walker Hale IV Mar 1 '16 at 22:51
  • 1
    @WalkerHaleIV Why are you creating an executable instead of just printing the escape sequence to stdout? – Chris Page Mar 2 '16 at 21:51

⌘K, shortcut to “View > Clear scrollback”.

UPDATE: This can be automated with AppleScript by the following command:

osascript -e 'tell application "System Events" to keystroke "k" using command down'

…which you can of course alias to whatever you want, or store in a function in your ~/.profile login script  :)

As a security though, to be able to use this even in background without risking to issue a keystroke to a wrong front app, I'd advise surrounding it with the following test:

if application "Terminal" is frontmost

…which gives us the following code:

osascript -e 'if application "Terminal" is frontmost then tell application "System Events" to keystroke "k" using command down'

…which in turns, properly escaped and aliased, ends in:

alias clear="osascript -e 'if application \"Terminal\" is frontmost then tell application \"System Events\" to keystroke \"k\" using command down'"

And here is your new clear!  :)

  • The point here is I'd like to set up something in my shell's environment so that when I run a command (say vim), the shell executes the "scrollback reset" before executing vim. Programmatic, not interactive. – Jason Salaz Nov 21 '11 at 18:46
  • 1
    @JasonSalaz Ok, didn't get that, sorry. Added code for that. – MattiSG Nov 21 '11 at 19:26
  • 1
    @JasonSalaz if this answer did solve your problem, please remember to validate it. If it didn't, please get back to us in the comments :) Remember, users from the future will thank you ;) – MattiSG Nov 25 '11 at 19:54
  • That comic is about me, you know. I do program in Denver, CO. (No, not really, it'd be insane if it were, though.) – Jason Salaz Nov 25 '11 at 20:25
  • This does not work for me. As far as I can tell, it is the equivalent of typing clear; that is, it clears the screen, but I can still scroll back. Hitting Command K works normally. – Zev Eisenberg Mar 8 '14 at 23:42

Here's code that works for both macOS' Terminal, and iTerm2. It doesn't need the window to be in the foreground, either.

printf '\033[2J\033[3J\033[1;1H'

Don't ask me how it works.

You must log in to answer this question.

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