iTerm2: Clear scrollback buffers for *all* sessions
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:
Open Automator and create a new Service.
Set "Service Receives" to "no input" and select "iTerm.app" as the application.
Put in a single Run Applescript action, and paste in the code from above.
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.
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).
Related videos on Youtube
kontinuity
Updated on September 18, 2022Comments
-
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 over 8 yearsGreat 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 over 8 yearsI don't know what to do about the flash, but I just replaced
keystroke "k"...
withclick menu item...
, which let me remove thedelay
. Also, it restoresmyCurrentSession
. Does that resolve the issues? -
kontinuity over 8 yearsThat didn't work at all
-
pjvandehaar over 8 yearsWhat goes wrong? Can you open Script Editor, paste in that code, run it, and tell me the error?
-
pjvandehaar over 8 yearsAre you on iTerm 2.1.4?
-
Igor Hatarist about 6 yearsThanks 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.