Skip to content

Instantly share code, notes, and snippets.

@nleush
Last active April 14, 2022 20:44
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 nleush/951a64724a0b2a5d3747bbe446183c4a to your computer and use it in GitHub Desktop.
Save nleush/951a64724a0b2a5d3747bbe446183c4a to your computer and use it in GitHub Desktop.
iTerm2: open multiple sessions and run commands, allow many tabs and 2 to 4 panes splits in one tab
#!/usr/bin/env python3.7
import iterm2
# Each top level item is new tab.
# Each second level item is new split pane session inside the tab.
windowsConfig = [
# Max 4 sessions in one tab.
['echo 41', 'echo 42', 'echo 43', 'echo 44'],
['echo 31', 'echo 32', 'echo 33'],
['echo 21', 'echo 22'],
['echo 11']
]
async def buildTab(tab, splitsConfig):
first_tab = tab.current_session
sessions = [first_tab]
if len(splitsConfig) == 4:
bottom_left = await first_tab.async_split_pane(vertical=False)
bottom_right = await bottom_left.async_split_pane(vertical=True)
top_right = await first_tab.async_split_pane(vertical=True)
sessions.append(top_right)
sessions.append(bottom_left)
sessions.append(bottom_right)
elif len(splitsConfig) == 3:
middle = await first_tab.async_split_pane(vertical=True)
right = await middle.async_split_pane(vertical=True)
sessions.append(middle)
sessions.append(right)
elif len(splitsConfig) == 2:
right = await first_tab.async_split_pane(vertical=True)
sessions.append(right)
else:
print("Only 4 tabs max supported")
for i in range(len(sessions)):
await sessions[i].async_send_text(splitsConfig[i] + '\n')
async def main(connection):
app = await iterm2.async_get_app(connection)
await iterm2.Window.async_create(connection)
window = app.current_terminal_window
if window is not None:
for i in range(len(windowsConfig)):
if i == 0:
tab = window.current_tab
else:
tab = await window.async_create_tab()
await buildTab(tab, windowsConfig[i])
else:
print("No current window")
iterm2.run_until_complete(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment