AgentSkillsCN

tmux-enter-key-handling

在向 tmux 会话发送输入时,尤其是针对像 Claude Code 或 Codex 这样的交互式 TUI,务必使用十六进制编码,而非直接按下 Enter 键——否则输入将悄然失效。

SKILL.md
--- frontmatter
name: tmux-enter-key-handling
description: Use when sending input to tmux sessions, especially interactive TUIs like Claude Code or Codex. The Enter keyword fails silently - must use hex codes.

Tmux Enter Key Handling

What This Solves

When sending input to tmux panes running interactive applications (Claude Code, Codex, vim, etc.), the Enter keyword in tmux send-keys fails silently. Text appears but is never submitted.

Working Approach

Send Enter as hex 0x0d (carriage return) in a separate command:

Bash

bash
# ✅ CORRECT - send text, then Enter as hex
tmux send-keys -t "session-name" "Hello world"
tmux send-keys -t "session-name" -H 0d

Python

python
import subprocess

# Send text
subprocess.run(['tmux', 'send-keys', '-t', session, prompt])

# Send Enter as hex (REQUIRED for TUIs)
subprocess.run(['tmux', 'send-keys', '-t', session, '-H', '0d'])

Other Special Keys

KeyHex CodeUsage
Enter0dtmux send-keys -t sess -H 0d
Escape1btmux send-keys -t sess -H 1b
Tab09tmux send-keys -t sess -H 09

Failed Attempts

⚠️ Read this first - These approaches don't work:

AttemptWhy It FailedTime Wasted
tmux send-keys -t sess "text" EnterEnter keyword ignored by TUI apps2+ hours debugging
subprocess.run([..., prompt, 'Enter'])Same issue - Python passes it but TUI ignores1 hour
Checking return code for successreturncode=0 means tmux ran, NOT that input was submitted30 min false confidence

Verification (Critical!)

Never trust return codes. Always verify visually:

bash
# Send prompt
tmux send-keys -t sess "Write a haiku"
tmux send-keys -t sess -H 0d

# Wait for response
sleep 5

# VERIFY the response actually appeared
tmux capture-pane -t sess -p -S -50

Only report success when you SEE:

  1. The prompt text in output
  2. Processing indicator (e.g., "Thinking...")
  3. Actual response content
  4. New input prompt ready

Environment & Dependencies

  • Applies to: Any tmux version
  • Affected apps: Claude Code, Codex CLI, vim, emacs, any ncurses/TUI app
  • Works fine with: Regular shell prompts (bash, zsh) - but hex is safer everywhere

Session Reference

  • Date: 2024
  • Source: tmux_sdk development, multiple debugging sessions
  • Original issue: Commands appeared typed but never executed