it2 session focused
Get the currently focused session ID
Synopsis
it2 session focused [flags]
Description
Get the session ID that currently has keyboard focus in iTerm2.
This queries iTerm2's focus state using the FocusRequest API, which may be
different from the current session (from $ITERM_SESSION_ID).
Use 'session current' to get the session where this command is running.
Use 'session focused' to get the session the user is currently viewing.
Options
-
--format(default "table") - Output format (table|json|yaml|text)
-
--timeout(default "5s") - Command timeout
-
--url(default "ws://localhost:1912") - iTerm2 WebSocket URL
See Also
- it2 session - Manage iTerm2 sessions
Examples
# Get the focused session ID
$ it2 session focused
w0t0p0:ABC123-XYZ789
# Send text to the focused session (even if running in background)
$ it2 session send-text $(it2 session focused) "hello"
# Get title of focused session
$ it2 session get-title $(it2 session focused)
# Compare current vs focused
$ echo "Running in: $(it2 session current)"
$ echo "User viewing: $(it2 session focused)"
# Build notification example: notify user in whatever session they're viewing
$ if make; then
$ it2 session send-text $(it2 session focused) "echo '✅ Build complete!'"
$ it2 session send-key $(it2 session focused) enter
$ fi
# JSON output
$ it2 session focused --format json