Focus iTerm2 sessions by ID
Project description
iterm2-focus
Focus iTerm2 sessions by ID from the command line.
Features
- Focus any iTerm2 session by its ID
- Get the current session ID
- List all available sessions
- Focus the current session (useful when returning from other applications)
Installation
pip install iterm2-focus
Or using uv:
uv pip install iterm2-focus
Quick Terminal Notification
Send a macOS notification that focuses the current iTerm2 session when clicked:
terminal-notifier -title "Task Complete" -message "Click to focus" -execute "$(which uvx) iterm2-focus $ITERM_SESSION_ID"
Note: Requires terminal-notifier (brew install terminal-notifier).
Quick Usage (without installation)
You can run iterm2-focus directly without installing using uvx:
# List all sessions
uvx iterm2-focus --list
# Focus a specific session
uvx iterm2-focus <session-id>
# Get current session ID
uvx iterm2-focus --get-current
Prerequisites
- macOS with iTerm2 installed
- Python 3.10 or later
- iTerm2 Python API must be enabled:
- Open iTerm2
- Go to Settings → General → Magic
- Check "Enable Python API"
- Restart iTerm2
Usage
Focus a specific session
iterm2-focus w0t0p0:12345678-1234-1234-1234-123456789012
Focus the current session
Useful when returning from another application:
iterm2-focus --current
# or
iterm2-focus -c
Get the current session ID
iterm2-focus --get-current
# or
iterm2-focus -g
List all sessions
iterm2-focus --list
# or
iterm2-focus -l
Additional options
# Show version
iterm2-focus --version
# Quiet mode (suppress output)
iterm2-focus -q <session-id>
# Help
iterm2-focus --help
MCP Server Mode
iterm2-focus can run as an MCP (Model Context Protocol) server, allowing LLM applications like Claude Desktop to control iTerm2 sessions.
Starting the MCP server
# Install with MCP support
pip install 'iterm2-focus[mcp]'
# Start the MCP server
iterm2-focus --mcp
Configuring Claude Desktop
Using Claude Code CLI (recommended):
# Add the MCP server using uvx (no installation required)
claude mcp add iterm2-focus uvx iterm2-focus --mcp
# Or if you have it installed locally
claude mcp add iterm2-focus iterm2-focus --mcp
Or manually add to ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"iterm2-focus": {
"command": "uvx",
"args": ["iterm2-focus", "--mcp"]
}
}
}
Or if you have it installed locally:
{
"mcpServers": {
"iterm2-focus": {
"command": "iterm2-focus",
"args": ["--mcp"]
}
}
}
Available MCP tools
- list_sessions: List all iTerm2 sessions with their IDs and metadata
- focus_session: Focus a specific session by ID
- get_current_session: Get information about the currently focused session
Examples
Save and restore focus
# Save current session ID
SAVED_SESSION=$(iterm2-focus -g -q)
# ... do other work ...
# Return to saved session
iterm2-focus $SAVED_SESSION
Focus a session from another application
-- AppleScript example
do shell script "iterm2-focus w0t0p0:12345678-1234-1234-1234-123456789012"
Development
Setup
# Clone the repository
git clone https://github.com/mkusaka/iterm2-focus
cd iterm2-focus
# Create virtual environment with uv
uv venv
uv pip install -e ".[dev]"
Testing
# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov=iterm2_focus
# Type checking
uv run mypy src
# Linting and formatting
uv run ruff check src tests
uv run black src tests
Building
# Build the package
uv build
# Upload to PyPI
uv publish
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Troubleshooting
"Failed to connect to iTerm2" error
Make sure iTerm2's Python API is enabled (see Prerequisites).
"Session not found" error
Verify the session ID using iterm2-focus --list to see all available sessions.
Permission errors
On first run, macOS may ask for permission to control iTerm2. Please allow this in System Preferences.
Author
mkusaka hinoshita1992@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iterm2_focus-0.0.13.tar.gz.
File metadata
- Download URL: iterm2_focus-0.0.13.tar.gz
- Upload date:
- Size: 133.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
276e8c24797695ecfd51e7b961388f1aa160151d1db739cba869b3bb28288be5
|
|
| MD5 |
c2f816896ceace5ca6bd6ba805c35ba6
|
|
| BLAKE2b-256 |
7d978987bdf2ec96302be7509e5926db1f179d0cd0d067a7cf689d0a919ce991
|
Provenance
The following attestation bundles were made for iterm2_focus-0.0.13.tar.gz:
Publisher:
ci.yml on mkusaka/iterm2-focus
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
iterm2_focus-0.0.13.tar.gz -
Subject digest:
276e8c24797695ecfd51e7b961388f1aa160151d1db739cba869b3bb28288be5 - Sigstore transparency entry: 926874549
- Sigstore integration time:
-
Permalink:
mkusaka/iterm2-focus@ab5edbcaca6beb9fdfeec4cc9392a43ef69734fa -
Branch / Tag:
refs/tags/v0.0.13 - Owner: https://github.com/mkusaka
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@ab5edbcaca6beb9fdfeec4cc9392a43ef69734fa -
Trigger Event:
push
-
Statement type:
File details
Details for the file iterm2_focus-0.0.13-py3-none-any.whl.
File metadata
- Download URL: iterm2_focus-0.0.13-py3-none-any.whl
- Upload date:
- Size: 25.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4913d4d4ea319604458aa44ec826edb3f157a072298c2ae9cf23728a052428b6
|
|
| MD5 |
c1ba6601c41781ea538a4f225884b964
|
|
| BLAKE2b-256 |
27e0b3961a6cf3ed68d152b49d8218195f1450bef4952f573ab3a6da726ebace
|
Provenance
The following attestation bundles were made for iterm2_focus-0.0.13-py3-none-any.whl:
Publisher:
ci.yml on mkusaka/iterm2-focus
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
iterm2_focus-0.0.13-py3-none-any.whl -
Subject digest:
4913d4d4ea319604458aa44ec826edb3f157a072298c2ae9cf23728a052428b6 - Sigstore transparency entry: 926874551
- Sigstore integration time:
-
Permalink:
mkusaka/iterm2-focus@ab5edbcaca6beb9fdfeec4cc9392a43ef69734fa -
Branch / Tag:
refs/tags/v0.0.13 - Owner: https://github.com/mkusaka
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@ab5edbcaca6beb9fdfeec4cc9392a43ef69734fa -
Trigger Event:
push
-
Statement type: