This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
STOP. READ THIS. THIS IS NON-NEGOTIABLE.
This tool is ONLY for managing WINDOWS and PANES within existing sessions. If you see yourself doing any of the following:
- Creating new tmux sessions
- Destroying tmux sessions
- Modifying session configuration
- Renaming sessions
- Managing session lifecycle in any way
YOU ARE DOING IT WRONG. STOP IMMEDIATELY.
- Windows and Panes Only: You can create/destroy/manage windows and panes. That's it. That's all you're allowed to do.
- Existing Sessions Only: Always operate within an existing tmux session. The tool automatically handles session detection.
- No Session Lifecycle: Never create or destroy entire sessions. Not once. Not ever. Not for any reason.
- Use Default Sessions: When outside tmux, the tool automatically uses the 'claude-dev' session. Don't override this unless explicitly instructed.
If you need a tmux session, assume it already exists. If it doesn't exist, fail gracefully and report the error to the user.
tmux-iterm-command is a command-line tool that bridges coding agents (Claude, Qwen, Gemini, Codex) with tmux. It enables coding agents to programmatically:
- Create and manage tmux windows and panes within existing sessions (WINDOWS AND PANES ONLY)
- Execute commands and capture output
- Interact with terminal sessions in a structured way
- Operate within existing tmux sessions (does not create/destroy entire sessions)
-
CLI Entry Point (
src/claude_tmux/cli.py)- Argument parsing using Click framework
- Command routing and delegation
- Main interface for external callers (coding agents)
-
Tmux Abstraction Layer (
src/claude_tmux/manager.py)- Uses libtmux library for tmux operations (no subprocess calls)
- Session detection and management within existing sessions
- Window and pane operations
- Command execution and output capture
-
Command Handlers (
src/claude_tmux/commands.py)- Individual command modules for each CLI command
- Each follows Click command pattern
- Encapsulates command logic and option parsing
User (Claude Code Agent) → CLI Arguments → Command Router → Handler → Tmux Binary/iTerm2 API → Terminal Session
# Install Python dependencies
pip install -e .
# Create a virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate
pip install -e .# Show available commands
tmux-iterm-command --help
# Run a specific command with options
tmux-iterm-command create-window --name editor
# For development: run directly with Python
python -m src.tmux_iterm_command.cli <command> <args># Show available commands
uvx git+https://github.com/joej/tmux-iterm-command --help
# Run a specific command with options
uvx git+https://github.com/joej/tmux-iterm-command create-window --name editor# Run all tests
pytest tests/
# Run specific test file
pytest tests/test_tmux_manager.py
# Run with coverage
pytest --cov=src tests/
# Run specific test
pytest tests/test_tmux_manager.py::test_create_windowtmux-iterm-command --help # Show all available commandsCore Commands:
capture-pane- Capture output from a panecreate-pane- Split a window to create a new panecreate-window- Create a new tmux windowdetect- Detect environment capabilitieskill-pane- Kill a tmux panekill-window- Kill a tmux windowlist-panes- List all panes in a windowlist-sessions- List all tmux sessionslist-windows- List all windows in a sessionsend- Send a command to a panestatus- Show current tmux statuswait-idle- Wait for a pane to be idle
Placeholder Commands (require iTerm2 features):
notify- Send notificationset-badge- Set iTerm2 badge for a windowset-mark- Set iTerm2 mark for a paneset-tab-color- Set iTerm2 tab color
# Create a window in the current/default session
tmux-iterm-command create-window --name editor --command "vim myfile.txt"
# Create a pane by splitting a window
tmux-iterm-command create-pane --window 0 --vertical --command "tail -f logs/app.log"
# List active sessions
tmux-iterm-command list-sessions
# List windows in current session
tmux-iterm-command list-windows
# List panes in a window
tmux-iterm-command list-panes --window 0# uvx syntax: uvx git+https://github.com/joej/tmux-iterm-command <command> [options]
# Create a window with command
uvx git+https://github.com/joej/tmux-iterm-command create-window --name my-window --command "ls -la"
# Send command to existing pane
uvx git+https://github.com/joej/tmux-iterm-command send --window 0 --pane 0 "ps aux"
# Capture pane output
uvx git+https://github.com/joej/tmux-iterm-command capture-pane --window 0 --pane 0 --lines 50
# Kill a window
uvx git+https://github.com/joej/tmux-iterm-command kill-window --window 1# Send a command to a specific pane (automatically adds Enter)
tmux-iterm-command send --window 0 --pane 0 "npm install"
# Send command without pressing Enter
tmux-iterm-command send --window 0 --pane 0 --no-enter "echo hello"
# Capture pane output
tmux-iterm-command capture-pane --window 0 --pane 0 --lines 100
# Wait for pane to be idle (no output for 2 seconds, timeout after 30s)
tmux-iterm-command wait-idle --window 0 --pane 0 --quiet-for 2 --timeout 30# Kill a window
tmux-iterm-command kill-window --window 1
# Kill a pane
tmux-iterm-command kill-pane --window 0 --pane 1
# Detect environment capabilities
tmux-iterm-command detect
# Show current status
tmux-iterm-command statusAll tmux interactions use the libtmux library, which provides a Pythonic interface to tmux. This ensures:
- Clean object-oriented API for tmux operations
- Better integration with Python error handling
- Consistent behavior across different environments
# Pattern: Use libtmux Server, Session, Window, and Pane objects
from libtmux import Server
server = Server()
session = server.sessions[0] # Get first session
window = session.new_window(window_name="my_window")Each command handler follows this pattern:
# src/claude_tmux/commands.py
import click
@click.command()
@click.option('--window', 'window_index', type=int, required=True, help='Window index')
@click.option('--pane', 'pane_index', type=int, required=True, help='Pane index')
def my_command(window_index, pane_index):
"""Do something in tmux."""
# Implementation
passNote: Some commands like create-window only require --name and optional --command, while commands like send require both --window and --pane.
All commands return structured output:
- Success:
{"status": "success", ...} - Error:
{"status": "error", "message": "...", "code": "..."} - Output goes to stdout as JSON; coding agents parse responses
The tool works within existing tmux sessions:
- Inside tmux: Uses the current session
- Outside tmux: Creates or attaches to a default session ('claude-dev')
- No session lifecycle: Does not create/destroy entire sessions, only manages windows/panes
- Add function to
src/tmux_iterm_command/commands.py(all commands are in one file currently) - Implement Click command with proper options
- Add to command registration in
src/tmux_iterm_command/cli.py - Write tests in
tests/test_commands.py - Test manually:
tmux-iterm-command your-command --help
- Unit tests for tmux wrapper functions (mock libtmux calls)
- Integration tests for actual tmux operations (in isolated test sessions)
- Use pytest fixtures for session/window/pane setup and teardown
- Tests validate JSON output format for coding agent consumption
# All commands support --verbose flag
tmux-iterm-command create-window --name editor --verbose
# Or set environment variable
TMUX_ITERM_COMMAND_DEBUG=1 tmux-iterm-command create-window --name editor# Inspect sessions created by the tool
tmux list-sessions
tmux list-windows -t session-name
tmux list-panes -t session-name:window-index
# Attach to a session for inspection
tmux attach-session -t session-name- "Tmux not found": Ensure tmux is installed (
brew install tmux) - Permission denied: Check tmux socket permissions in
/tmp - Command hangs: Use appropriate timeout settings
- Session detection: Tool works inside or outside tmux, using appropriate session
- Python 3.8+
- tmux (CLI binary for libtmux to interface with)
- libtmux: Python library for tmux control
- click: CLI framework
Core Python packages:
libtmux: Tmux control libraryclick: CLI frameworkpytest: Testing framework
See requirements.txt for full list and versions.