iTerm2: Clear scrollback buffers for *all* sessions

7,027

Solution 1

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

Solution 2

The accepted answer didn't work for me. I'm using iTerm2 Build 3.0.15.

Here's what worked, after binding the desired action (in my case, Clear Buffer) to F12:

tell application "iTerm"
    set currentWindow to the current window
    set currentTab to the current tab of the current window
    set currentSession to the current session of the current tab of the current window
    repeat with aWindow in windows
        tell aWindow
            activate
            repeat with theTab in tabs of aWindow
                tell theTab
                    select
                    repeat with theSession in sessions of theTab
                        tell theSession
                            select
                            tell application "System Events" to tell process "iTerm"
                                tell application "System Events"
                                    key code 111
                                end tell
                            end tell
                        end tell
                    end repeat
                end tell
            end repeat
        end tell
    end repeat
    select currentWindow
    select currentTab
    select currentSession
end tell

This is a useful tool as I tend to keep logs running all day and night. I schedule this to run via crontab every morning so I come in to work with a clean slate and some freed up memory (which, depending on the log file(s) I'm tailing, can amount to gigabytes).

Share:
7,027

Related videos on Youtube

kontinuity
Author by

kontinuity

Updated on September 18, 2022

Comments

  • kontinuity
    kontinuity over 1 year

    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!

  • kontinuity
    kontinuity over 8 years
    Great start! This worked albeit with a really ugly flash and constant session window switch which at some point makes it unusable. However accepting this as an answer which others can improve upon.
  • pjvandehaar
    pjvandehaar over 8 years
    I don't know what to do about the flash, but I just replaced keystroke "k"... with click menu item..., which let me remove the delay. Also, it restores myCurrentSession. Does that resolve the issues?
  • kontinuity
    kontinuity over 8 years
    That didn't work at all
  • pjvandehaar
    pjvandehaar over 8 years
    What goes wrong? Can you open Script Editor, paste in that code, run it, and tell me the error?
  • pjvandehaar
    pjvandehaar over 8 years
    Are you on iTerm 2.1.4?
  • Igor Hatarist
    Igor Hatarist about 6 years
    Thanks a lot, it's working great! I hope you don't mind, I added a currentSession variable to (re-)store the active window split within the active tab.