iTerm2 Python API Split Tab

Viewed 942

Using the iTerm Python API added in the 3.3.0 release, is it possible to split a tab horizontally? The same as;

Right Click -> Split Pane Horizontally


import iterm2

async def main(connection):

    # Get app
    app = await iterm2.async_get_app(connection)

    # current window
    window = app.current_terminal_window
    if window is not None:

        # Create main & sub tab
        main = await window.async_create_tab()
        await main.async_set_title('~ MAIN ~')
        sub = await window.async_create_tab()
        await sub.async_set_title('~ SUB ~')

        # Split sub
        ...


Sending the default keystroke (command + d) doesn't seem possible.

1 Answers

iTerm Python API has an async_split_pane method that can split panes:

tab = await window.async_create_tab()
session = tab.current_session
splited = await session.async_split_pane(vertical=False)

Session is responsible for splitting a tab. If you want to split the newly created pane then do following:

await splited.async_activate()
await session2.async_split_pane(vertical=True)

Your example:

#!/usr/bin/env python3.7
    
import iterm2


async def main(connection):

    # Get app
    print("Test::main")
    app = await iterm2.async_get_app(connection)

    # Ensure window
    window = app.current_terminal_window
    if app.current_terminal_window is None:
        exit()

    # Create main tab
    main = await window.async_create_tab()
    await main.async_activate()
    await main.async_set_title('~ Title ~')
    sess = main.current_session

    # Split main
    sub = await sess.async_split_pane(vertical=True)
    await sub.async_send_text('whoami\n')

iterm2.run_until_complete(main)
Related