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.


4 Answers 4


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_PROGRAM is Apple_Terminal before sending it.)

  • 6
    This is so much faster than osascript!
    – Sergei
    Commented May 28, 2014 at 13:35
  • 6
    This is a much better answer. So, to make this permanent, add to your ~/.bash_profile: alias clear="clear && printf '\e[3J'"
    – Tom
    Commented Jun 15, 2015 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 Commented Mar 1, 2016 at 22:51
  • 2
    @WalkerHaleIV Why are you creating an executable instead of just printing the escape sequence to stdout?
    – Chris Page
    Commented Mar 2, 2016 at 21:51
  • 1
    – pbatey
    Commented Jul 5, 2019 at 19:07

⌘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. Commented Nov 21, 2011 at 18:46
  • 1
    @JasonSalaz Ok, didn't get that, sorry. Added code for that.
    – MattiSG
    Commented Nov 21, 2011 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
    Commented Nov 25, 2011 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.) Commented Nov 25, 2011 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. Commented Mar 8, 2014 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 (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:

  1. \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).

  2. \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.

  3. \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.

  • Can I ask you where you got it from? +1 from me - it works anyway
    – rbrtl
    Commented May 4, 2019 at 3:54
  • 1
    I don't remember, but it's a composition of these: en.wikipedia.org/wiki/ANSI_escape_code
    – Alexander
    Commented May 4, 2019 at 16:38
  • 1
    Looks like it's "CSI n J" (where n = 2) + "CSI n J" (where n = 3) + "CSI n ; m H" (where n, m = 1, 1), which translates to: "ED - Erase in Display" (variant 2), "ED - Erase in Display" (variant 3), "CUP - Cursor Position" (moving to 1, 1)
    – Alexander
    Commented May 4, 2019 at 16:42
  • A bit shorter: printf '\e[2J\e[3J\e[;H'.
    – done
    Commented Mar 14, 2020 at 18:28
  • 1
    Explanation: A \e is (usually) the same a \033, and, as the default of CSI H is 1;1 there is no need to actually write \e[1;1H but \e[;H (or even e[H) will do the same. It is the same command you wrote but with less characters to type.
    – done
    Commented Mar 14, 2020 at 20:04

I'm not sure if this works on OS X but for Linux the best option seems to be tput reset.

  • This doesn't work in mac Terminal.app or iTerm2.app but, interestingly, it does work in VSCode's integrated terminal.
    – Denis Howe
    Commented Dec 6, 2023 at 11:54
  • I guess VS Code integrated terminal successfully emulates VT terminal and Terminal.app does something else. Commented Dec 7, 2023 at 11:57

You must log in to answer this question.

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