Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 9 down vote accepted

⌘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!  :)

share|improve this answer
    
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
    
@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 at 23:42
show 4 more comments

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

share|improve this answer
    
This is so much faster than osascript! –  Sergei May 28 at 13:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.