Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get and set the titles of the window, tab, and session in iTerm 2 (with the API)?

I've been looking through the documentation for iTerm2's Python API, trying to figure out a way to get and set the names/titles of

  • windows,
  • tabs, and
  • sessions.

I've also experimented in an iTerm Python REPL, to no avail.

Window title seems to still be settable with the old shell escape codes, e.g. using a function like this:

window_title() {
    echo -ne "\e]2;$@\a\e]1;$@\a"
}

But I can't even find alternative ways of setting the session title or the tab title, much less doing so through the Python API.

The relationship between window title, tab title, and session title can be more clearly seen if you look at its effects (assuming session title is set to show the session name--a distinction that can be confusing if they are not set to be the same).

The relationship between window title, tab title, and session title

like image 730
iconoclast Avatar asked Oct 31 '25 16:10

iconoclast


1 Answers

Reading:

# https://iterm2.com/python-api/session.html#iterm2.Session.async_get_variable
session_title = (await session.async_get_variable("autoName"))

Setting:

# https://iterm2.com/python-api/session.html#iterm2.Session.async_set_name
await session.async_set_name(session_title + "!")

Here is a more detailed example:

async def get_all_sessions(app:iterm2.app.App):
    for window in app.windows:
        window_title = (await window.async_get_variable("titleOverride"))
        print("window title: %s" % (window_title))
        # add window title suffix
        # await window.async_set_title(window_title+" ;)")
        # remove the added window title suffix
        # await window.async_set_title(window_title[0:-3])
        # print("window detailed: %s" % (window.pretty_str()))
        for tab in window.tabs:
            tab_title = (await tab.async_get_variable("titleOverride"))
            print("\t\ttab title: %s" % (tab_title))
            for session in tab.sessions:
                # https://iterm2.com/documentation-session-title.html
                # https://stackoverflow.com/questions/56069701/how-do-i-get-and-set-the-titles-of-the-window-tab-and-session-in-iterm-2-with
                # https://iterm2.com/python-api/session.html#iterm2.Session.async_get_variable
                session_title = (await session.async_get_variable("autoName"))
                print("\t\t\tsession title: %s" % (session_title))
                if session_title == "colabo-GIST":
                    print("\t\t\t: changing session name: %s" % (session_title))
                    # https://iterm2.com/python-api/session.html#iterm2.Session.async_set_name
                    await session.async_set_name(session_title + "!")
                # await session.async_inject(code)
like image 156
mPrinC Avatar answered Nov 03 '25 08:11

mPrinC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!