Skip to content

Instantly share code, notes, and snippets.

@eliocapelati
Last active October 17, 2024 21:35
Show Gist options
  • Save eliocapelati/3a8fdcc54d06b684360b6e342af10593 to your computer and use it in GitHub Desktop.
Save eliocapelati/3a8fdcc54d06b684360b6e342af10593 to your computer and use it in GitHub Desktop.
open 4 panes in the same window using the current session and execute a command there. This is super handy when working in a project with multiple steps required to run and monitor it. Check out here how to use it: https://iterm2.com/python-api/tutorial/running.html
import iterm2
WORKING_DIRECTORY = "/path/to/your/folder"
async def main(connection):
app = await iterm2.async_get_app(connection)
window = app.current_terminal_window
if window is not None:
# Get the current session (pane)
session = window.current_tab.current_session
# Step 1: Split the current pane vertically (left and right panes)
left_pane = session
right_pane = await session.async_split_pane(vertical=True)
# Step 2: Split the left pane horizontally (top-left and bottom-left)
top_left_pane = left_pane
bottom_left_pane = await top_left_pane.async_split_pane(vertical=False)
# Step 3: Split the right pane horizontally (top-right and bottom-right)
top_right_pane = right_pane
bottom_right_pane = await top_right_pane.async_split_pane(vertical=False)
# Step 4: Send 'cd' commands to set the working directory for each pane
await top_left_pane.async_send_text(f"cd {WORKING_DIRECTORY}\n")
await bottom_left_pane.async_send_text(f"cd {WORKING_DIRECTORY}\n")
await top_right_pane.async_send_text(f"cd {WORKING_DIRECTORY}\n")
await bottom_right_pane.async_send_text(f"cd {WORKING_DIRECTORY}\n")
# Step 5: Send commands to each pane
await top_left_pane.async_send_text("echo 'Top Left Pane'\n")
await bottom_left_pane.async_send_text("echo 'Bottom Left Pane'\n")
await top_right_pane.async_send_text("echo 'Top Right Pane'\n")
await bottom_right_pane.async_send_text("echo 'Bottom Right Pane'\n")
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