iTerm2 is my default terminal and have been using it for close to 8 years now. It is highly customizable and has API support for hackability.
I was using it today to run a webpack watch command on my 2nd monitor. For every change, webpack recompiles the modified files and adds it to the output. Since every output looks the same, it wasn’t really clear if my change has been re-compiled or not.
I wrote a small script that will clear the scrollback of the current tab session every 10 seconds. This will ensure me that the last re-compilation step is the only one shown on the view.
Here are the steps:
- Open iTerm2
- Scripts -> Manage scripts -> New Python script
- Basic -> Simple
- Choose the default location and type
clear_scrollback_on_schedulefor the name
- Paste the following code in the text editor opened and save
- Open the tab where you want to execute the script and press
Shift + Cmd + O(shortcut for
Open Quickly) and type
scrolland you should see the new script. Press
Enterto launch the script
- Now the current tab’s scrollback should be cleared every 10 seconds!
Here is the full script:
#!/usr/bin/env python3.7 import iterm2 import time async def main(connection): app = await iterm2.async_get_app(connection) window = app.current_terminal_window if window is not None: tab = window.current_tab if tab is not None: while True: for session in tab.sessions: code = b'\x1b' + b']1337;ClearScrollback' + b'\x07' await session.async_inject(code) time.sleep(10) else: print("No current tab") else: print("No current window") iterm2.run_until_complete(main)
Reference – https://iterm2.com/python-api/examples/cls.html
Open Quickly window: