Documentation
¶
Overview ¶
Package it2 provides a powerful CLI tool and Go client library for the iTerm2 API.
The it2 command-line tool enables comprehensive programmatic control of iTerm2 on macOS. For library usage, see the github.com/tmc/it2/internal/client package. All major iTerm2 automation features are supported through Unix socket and WebSocket APIs.
Features ¶
Comprehensive iTerm2 control capabilities:
- Session Management: Create, list, close, split, activate, restart, monitor
- Tab Management: Create, list, close, move, activate tabs
- Window Management: Create, list, close, focus, arrange windows
- Text Operations: Send text, manipulate buffers, control cursor, search
- Shell Integration: Access command history, prompts, job monitoring
- Variable Management: Get/set with session, tab, window, app scopes
- Profile Management: List profiles, get/set properties dynamically
- Color Management: Import/export presets, modify appearance
- Notifications: Subscribe to and monitor iTerm2 events in real-time
- tmux Integration: Control tmux sessions through iTerm2
- Broadcast Domains: Manage input broadcasting to multiple sessions
- Arrangements: Save and restore window arrangements
Installation ¶
Install the CLI tool:
go install github.com/tmc/it2/cmd/it2@latest
For library usage:
import "github.com/tmc/it2/internal/client"
Library Usage ¶
Use the internal/client package for programmatic access:
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/it2/internal/client"
)
func main() {
// Create and connect to iTerm2
c := client.New("ws://localhost:1912")
ctx := context.Background()
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.Close()
// List all sessions
sessions, err := c.ListSessions(ctx)
if err != nil {
log.Fatal(err)
}
for _, session := range sessions {
fmt.Printf("Session: %s (Window: %s)\n",
session.SessionID, session.WindowID)
}
// Send text to first session
if len(sessions) > 0 {
err = c.SendText(ctx, sessions[0].SessionID, "echo Hello!\n")
if err != nil {
log.Fatal(err)
}
}
}
Connection ¶
The client automatically chooses the best connection method:
- Unix socket: ~/Library/Application Support/iTerm2/private/socket (preferred, faster)
- WebSocket: ws://localhost:1912 (fallback if Unix socket unavailable)
Authentication ¶
The client automatically handles iTerm2 authentication:
- Auto-requests credentials from iTerm2 when needed (via AppleScript)
- iTerm2 prompts user to allow API access on first connection
- Credentials cached for session duration
- Falls back to ITERM2_COOKIE and ITERM2_KEY environment variables
- Can be bypassed entirely in trusted environments (see auth package)
Advanced Features ¶
Shell Integration - Access command history and prompts:
// List command history with exit codes
prompts, err := client.ListPrompts(ctx, sessionID)
for _, promptID := range prompts {
details, _ := client.GetPromptByID(ctx, sessionID, promptID)
fmt.Printf("Command: %s, Exit: %d\n",
details.Command, details.ExitStatus)
}
Real-time monitoring of events:
// Subscribe to keystroke notifications
ch, err := client.SubscribeToNotifications(ctx, sessionID, "keystroke")
for notification := range ch {
// Process keystroke events
fmt.Printf("Key pressed: %v\n", notification)
}
Variable management with scoping:
// Set user variable at session scope err = client.SetVariableWithScope(ctx, "session", sessionID, "user.myvar", "value") // Get app-level variable pid, err := client.GetVariableWithScope(ctx, "app", "", "pid")
CLI Tool ¶
The it2 command provides comprehensive terminal control.
Usage:
it2 [command]
Available Commands:
app Control the iTerm2 application arrangement Manage iTerm2 window arrangements auth Manage iTerm2 API authentication broadcast Manage broadcast domains color Manage iTerm2 colors and appearance completion Generate the autocompletion script for the specified shell help Help about any command job Monitor jobs in iTerm2 sessions notification Subscribe to iTerm2 notifications profile Manage iTerm2 profiles prompt Manage prompts and command history in iTerm2 sessions selection Text selection operations in iTerm2 session Manage iTerm2 sessions statusbar Manage iTerm2 status bar components tab Manage iTerm2 tabs text Text and buffer operations in iTerm2 tmux Manage tmux integration variable Manage iTerm2 variables window Manage iTerm2 windows
Global Flags:
--format string Output format (text, json, yaml) (default "text") --timeout duration Connection timeout (default 5s) --url string API URL (default "ws://localhost:1912", auto-detects Unix socket)
Examples:
# List all sessions it2 session list # Send text to current session (uses $ITERM_SESSION_ID) it2 session send-text "echo Hello, iTerm2!" # Show command history with Shell Integration it2 prompt list # Create a new tab it2 tab create # Split current pane vertically it2 session split --vertical # Monitor keystroke events in real-time it2 notification monitor --type keystroke # Get terminal buffer contents it2 text get-buffer <session-id> # Search command history it2 prompt search "git commit"
Use "it2 [command] --help" for more information about a command.
Environment Variables ¶
- ITERM_SESSION_ID: Current session ID (set by iTerm2)
- ITERM2_COOKIE: Authentication cookie (optional)
- ITERM2_KEY: Authentication key (optional)
- ITERM2_DEBUG: Enable debug logging when set to "1"
Session Identification ¶
Sessions can be identified using either format:
- Full: "w0t1p12:C3D91F33-3805-47E2-A3F6-B8AED6EC2209"
- UUID only: "C3D91F33-3805-47E2-A3F6-B8AED6EC2209"
Many commands support automatic fallback to $ITERM_SESSION_ID.
Requirements ¶
- iTerm2 version 3.3.0 or later
- Python API enabled: Preferences → General → Magic → Enable Python API
- macOS (iTerm2 is macOS-only)
See Also ¶
- Examples: https://github.com/tmc/it2/tree/main/examples
- iTerm2 API Docs: https://iterm2.com/documentation-api.html
- Protocol Buffers: proto/api.proto in source repository
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gen-docs
command
|
|
|
it2
command
Command it2 provides comprehensive control over iTerm2 through its WebSocket API.
|
Command it2 provides comprehensive control over iTerm2 through its WebSocket API. |
|
internal
|
|
|
cmd
Package cmd provides command implementations for the it2 CLI.
|
Package cmd provides command implementations for the it2 CLI. |
|
cmd/alert
Package alert provides iTerm2 alert and dialog commands.
|
Package alert provides iTerm2 alert and dialog commands. |
|
cmd/app
Package app provides commands for controlling the iTerm2 application.
|
Package app provides commands for controlling the iTerm2 application. |
|
cmd/auth
Package auth provides commands for iTerm2 API authentication.
|
Package auth provides commands for iTerm2 API authentication. |
|
cmd/color
Package color provides commands for managing iTerm2 colors and appearance.
|
Package color provides commands for managing iTerm2 colors and appearance. |
|
cmd/colorpreset
Package colorpreset provides commands for managing iTerm2 color presets.
|
Package colorpreset provides commands for managing iTerm2 color presets. |
|
cmd/control
Package control provides iTerm2 custom control management commands.
|
Package control provides iTerm2 custom control management commands. |
|
cmd/event
Package event provides commands for managing iTerm2 event subscriptions.
|
Package event provides commands for managing iTerm2 event subscriptions. |
|
cmd/filepanel
Package filepanel provides iTerm2 file panel dialog commands.
|
Package filepanel provides iTerm2 file panel dialog commands. |
|
cmd/focus
Package focus provides commands for managing focus in iTerm2.
|
Package focus provides commands for managing focus in iTerm2. |
|
cmd/job
Package job provides commands for job monitoring in iTerm2.
|
Package job provides commands for job monitoring in iTerm2. |
|
cmd/keyboard
Package keyboard provides commands for managing keyboard bindings in iTerm2.
|
Package keyboard provides commands for managing keyboard bindings in iTerm2. |
|
cmd/lifecycle
Package lifecycle provides iTerm2 session lifecycle management commands.
|
Package lifecycle provides iTerm2 session lifecycle management commands. |
|
cmd/notification
Package notification provides commands for iTerm2 notification system.
|
Package notification provides commands for iTerm2 notification system. |
|
cmd/preference
Package preference provides iTerm2 preference management commands.
|
Package preference provides iTerm2 preference management commands. |
|
cmd/profile
Package profile provides commands for iTerm2 profile management.
|
Package profile provides commands for iTerm2 profile management. |
|
cmd/prompt
Package prompt provides commands for managing prompts and command history in iTerm2 sessions using Shell Integration features.
|
Package prompt provides commands for managing prompts and command history in iTerm2 sessions using Shell Integration features. |
|
cmd/selection
Package selection provides commands for managing text selection in iTerm2.
|
Package selection provides commands for managing text selection in iTerm2. |
|
cmd/session
Package session provides commands for iTerm2 session management.
|
Package session provides commands for iTerm2 session management. |
|
cmd/shell
Package shell provides commands for shell state detection and prompt tracking in iTerm2 sessions using Shell Integration features.
|
Package shell provides commands for shell state detection and prompt tracking in iTerm2 sessions using Shell Integration features. |
|
cmd/shortcuts
Package shortcuts provides top-level convenience commands that wrap commonly used session commands.
|
Package shortcuts provides top-level convenience commands that wrap commonly used session commands. |
|
cmd/snippet
Package snippet provides text snippet storage and retrieval commands.
|
Package snippet provides text snippet storage and retrieval commands. |
|
cmd/tab
Package tab provides commands for iTerm2 tab management.
|
Package tab provides commands for iTerm2 tab management. |
|
cmd/text
Package text provides commands for text operations in iTerm2.
|
Package text provides commands for text operations in iTerm2. |
|
cmd/utility
Package utility provides iTerm2 utility and helper commands.
|
Package utility provides iTerm2 utility and helper commands. |
|
cmd/variable
Package variable provides commands for iTerm2 variable management.
|
Package variable provides commands for iTerm2 variable management. |
|
cmd/window
Package window provides commands for iTerm2 window management.
|
Package window provides commands for iTerm2 window management. |
|
cmdcore
Package cmdcore provides core command infrastructure for iTerm2 CLI operations.
|
Package cmdcore provides core command infrastructure for iTerm2 CLI operations. |
|
cmderr
Package cmderr provides standardized error handling for iTerm2 CLI commands.
|
Package cmderr provides standardized error handling for iTerm2 CLI commands. |
|
cmdutil
Package cmdutil provides command utilities for iTerm2 CLI operations
|
Package cmdutil provides command utilities for iTerm2 CLI operations |
|
iterm2settings
Package iterm2settings provides utilities for checking iTerm2 application state and settings.
|
Package iterm2settings provides utilities for checking iTerm2 application state and settings. |
|
jqutil
Package jqutil provides jq filtering utilities for JSON output.
|
Package jqutil provides jq filtering utilities for JSON output. |
|
scope
Package scope provides session filtering based on scope settings.
|
Package scope provides session filtering based on scope settings. |
|
sessionid
Package sessionid provides utilities for managing iTerm2 session identifiers.
|
Package sessionid provides utilities for managing iTerm2 session identifiers. |
|
validate
Package validate provides validation functions for iTerm2 CLI command inputs.
|
Package validate provides validation functions for iTerm2 CLI command inputs. |
|
waitstable
Package waitstable provides utilities for detecting when a stream or buffer has stabilized (no changes for a configured duration).
|
Package waitstable provides utilities for detecting when a stream or buffer has stabilized (no changes for a configured duration). |