2

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!

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
  • 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. – kontinuity Feb 17 '16 at 4:57
  • 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? – pjvandehaar Feb 17 '16 at 10:10
  • That didn't work at all – kontinuity Feb 17 '16 at 10:21
  • What goes wrong? Can you open Script Editor, paste in that code, run it, and tell me the error? – pjvandehaar Feb 17 '16 at 10:28
  • Are you on iTerm 2.1.4? – pjvandehaar Feb 17 '16 at 10:45
1

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

  • 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. – Igor Hatarist Mar 29 '18 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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