Skip to content

Instantly share code, notes, and snippets.

@gnachman
Created July 3, 2018 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnachman/a5010ca9af34edcb1d21c2648232aa6c to your computer and use it in GitHub Desktop.
Save gnachman/a5010ca9af34edcb1d21c2648232aa6c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import asyncio
import iterm2
import sys
# This script was created with the "basic" environment which does not support adding dependencies
# with pip.
async def main(connection, argv):
app = await iterm2.async_get_app(connection)
# Create four split panes and make the bottom left one active.
bottomLeft = app.current_terminal_window.current_tab.current_session
bottomRight = await bottomLeft.async_split_pane(vertical=True)
topLeft = await bottomLeft.async_split_pane(vertical=False, before=True)
topRight = await bottomRight.async_split_pane(vertical=False, before=True)
await bottomLeft.async_activate()
broadcast_to = [ topLeft, topRight, bottomRight ]
async def async_handle_keystroke(notification):
"""Called on each keystroke with iterm2.api_pb2.KeystrokeNotification"""
if notification.keyCode == 0x35:
# User pressed escape. Terminate script.
return True
for session in broadcast_to:
await session.async_send_text(notification.characters)
return False
# Construct a pattern that matches all keystrokes except those with a Command modifier.
# This prevents iTerm2 from handling them.
pattern = iterm2.KeystrokePattern()
pattern.keycodes.extend(range(0, 65536))
pattern.forbidden_modifiers.extend([iterm2.MODIFIER_COMMAND])
# This will block until async_handle_keystroke returns True.
async with bottomLeft.get_keystroke_reader(patterns_to_ignore=[pattern]) as reader:
done = False
while not done:
for keystroke in await reader.async_get():
done = await async_handle_keystroke(keystroke)
if done:
break
if __name__ == "__main__":
iterm2.Connection().run(main, sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment