关于 tmux
一项用于通过 tmux 操作交互式终端程序的 skill,适合需要在终端中运行命令、发送按键并查看输出的开发者。它涵盖交互式会话、远程执行及辅助脚本,并规定启动会话后的监控提示。
主要功能
- 01创建并控制 tmux 会话
- 02向终端发送命令和按键
- 03捕获终端输出
- 04通过 SSH 进行远程执行
- 05提供会话查找与文本轮询脚本
适用场景
- 01运行 Python、Node 或 psql REPL
- 02在终端中调试 gdb 或 lldb
- 03操作需要 TTY 的命令行程序
- 04通过 SSH 观察远程命令输出
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ connorads/ dotfiles --skill tmux安装后在 Claude Code 中输入/tmux确认已加载。
附带文件scripts/find-sessions.shscripts/wait-for-text.sh
详细介绍
tmux 技能
使用 tmux 通过发送按键和捕获输出来控制交互式终端应用程序。
适用场景
- 运行交互式 REPL(python、node、psql)
- 使用 gdb/lldb 调试
- 需要 TTY 交互的任何 CLI
- 需要观察输出的远程执行
核心模式
# Create session
tmux new-session -d -s "$SESSION" -x 120 -y 40
# Send commands
tmux send-keys -t "$SESSION" "python3" Enter
# Capture output
tmux capture-pane -t "$SESSION" -p
# Wait for prompt (poll)
for i in {1..30}; do
output=$(tmux capture-pane -t "$SESSION" -p)
if echo "$output" | grep -q ">>>"; then break; fi
sleep 0.5
done
# Cleanup
tmux kill-session -t "$SESSION"
远程执行(Codespaces/SSH)
对于通过 mise 安装的工具,请用 zsh 包装:
# Non-interactive (won't hang)
ssh host 'zsh -c "source ~/.zshrc; tmux new-session -d -s mysession; tmux send-keys -t mysession python Enter"'
# Interactive (for tmux attach) - needs TTY
ssh host -t 'zsh -ilc "tmux attach -t mysession"'
重要:使用 zsh -c "source ~/.zshrc; ...",而不是 zsh -lc,以避免卡住。
用户通知
启动会话后,始终打印:
To monitor: tmux attach -t $SESSION
To capture: tmux capture-pane -t $SESSION -p
提示
- 使用
-x 120 -y 40设置一致的窗格大小 - 使用
capture-pane -p轮询,而不是wait-for - 使用
-l标志发送字面文本,以避免 shell 展开 - 控制键:
C-c(中断)、C-d(EOF)、Escape - 对于 Python REPL:设置
PYTHON_BASIC_REPL=1,以避免花哨的控制台造成干扰
辅助脚本
wait-for-text.sh
轮询 tmux 窗格,查找文本模式并设置超时:
scripts/wait-for-text.sh -t session:0.0 -p '^>>>' -T 15
find-sessions.sh
列出 tmux 会话,可选择筛选:
scripts/find-sessions.sh -q claude # filter by name
scripts/find-sessions.sh --all # all sessions
评论
支持 Markdown