ITerm2: Clear scrollback buffers for *all* sessions

iterm2keyboard shortcuts

In iTerm2 (I am using Build 2.1.4 as of this writing), how can I clear scrollback buffers for all sessions. I know that cmd+K will clear current session. Using the same key combination does not work along with broadcast input.

Appreciate the help!

Best Answer

This applescript should clear scrollback for all sessions in the current iTerm window:

tell application "iTerm"
    tell current terminal
        activate
        set myCurrentSession to the current session
        repeat with theSession in sessions
            tell theSession
                select
                tell application "System Events" to tell process "iTerm"
                    click menu item "Clear Buffer" of menu 1 of menu bar item "Edit" of menu bar 1
                end tell
            end tell
        end repeat
        select myCurrentSession
    end tell
end tell

To bind that to a keyboard shortcut:

  1. Open Automator and create a new Service.

  2. Set "Service Receives" to "no input" and select "iTerm.app" as the application.

  3. Put in a single Run Applescript action, and paste in the code from above.

  4. Save it as clear-all-scrollback-buffers-in-current-iterm-window.

    Now when iTerm is open, you'll see that service in the menubar under iTerm > Services.

  5. Open System Preferences > Keyboard > Shortcuts > Services. Scroll down until you see clear-all-scrollback-buffers-in-current-iterm-window. Give it a keyboard shortcut (eg, optioncommandk)

If that doesn't work, you might try the following AppleScript instead:

tell application "iTerm"
    tell current terminal
        activate
        repeat with theSession in sessions
            tell theSession
                select
                tell application "System Events"
                    delay 0.1
                    keystroke "k" using {command down}
                end tell
            end tell
        end repeat
    end tell
end tell
Related Question