Skip to content

Instantly share code, notes, and snippets.

@lparry
Last active April 13, 2023 01:31
Show Gist options
  • Save lparry/991690e44e991b23eddd677c0e96bdfd to your computer and use it in GitHub Desktop.
Save lparry/991690e44e991b23eddd677c0e96bdfd to your computer and use it in GitHub Desktop.
Iterm2 python api script for reruning the last command in a special tab, to be bound to a global shortcut
#!/usr/bin/env python3.7
import iterm2
# This iterm 2 script can be bound to a global shortcut so you can rerun the
# previous command in a special tab without switching focus
async def main(connection):
app = await iterm2.async_get_app(connection)
session_id = await app.async_get_variable("user.rspec")
session = None
if session_id is not None:
session = app.get_session_by_id(session_id)
if session is None:
window = app.current_window
if window is not None:
tab = await window.async_create_tab()
await tab.async_set_title("(repeat-last-command)")
session = tab.current_session
session_id = session.session_id
await app.async_set_variable("user.rspec", session_id)
clear_session = b'\x1b' + b']1337;ClearScrollback' + b'\x07'
await session.async_inject(clear_session)
await session.async_send_text('!!\n\n')
iterm2.run_until_complete(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment