返回 Skill 列表
extension
分类: 开发与工程无需 API Key

tmux

使用tmux管理并发进程。在运行服务器、长时间任务或在不同面板中协调多个代理时使用。对于安全地向其他tmux面板发送多行文本或指令至关重要。

person作者: jakexiaohubgithub

Tmux Skill

This skill empowers you to manage multiple concurrent processes (like servers, watchers, or long builds) using tmux directly from the Bash tool.

Since you are likely already running inside a tmux session, you can spawn new windows or panes to handle these tasks without blocking your main communication channel.

1. Verify Environment & Check Status

First, verify you are running inside tmux:

echo $TMUX

If this returns empty, you are not running inside tmux and these commands will not work as expected.

Once verified, check your current windows:

tmux list-windows

2. Spawn a Background Process

To run a command (e.g., a dev server) in a way that persists and can be inspected:

  1. Create a new detached window with a specific name. This keeps it isolated and easy to reference.

    tmux new-window -n "server-log" -d
    

    (Replace "server-log" with a relevant name for your task)

  2. Send the command to that window.

    tmux send-keys -t "server-log" "npm start" C-m
    

    (C-m simulates the Enter key)

3. Sending Text to Panes

Warning: send-keys interprets control sequences. Multi-line text or text with special characters can trigger tmux modes (like C-r for search). Use the right method:

| Content Type | Method | |--------------|--------| | Simple shell command | send-keys "cmd" C-m | | Single line, may have special chars | send-keys -l "text" then send-keys C-m | | Multi-line text or instructions | load-buffer + paste-buffer |

Literal mode (-l flag) prevents interpreting escape sequences:

tmux send-keys -l -t "target" "text with C-r and other chars"
tmux send-keys -t "target" C-m

Multi-line content — always use load-buffer:

cat > /tmp/msg.txt << 'EOF'
Your multi-line content here.
Can include any characters safely.
EOF
tmux load-buffer /tmp/msg.txt && tmux paste-buffer -t "target"
tmux send-keys -t "target" C-m

4. Interacting with Other Agents

When sending instructions to another Claude instance running in a tmux pane:

cat > /tmp/instructions.txt << 'EOF'
Fix the authentication bug in src/auth.ts:
1. The token validation is missing null checks
2. Add proper error handling for expired tokens
EOF
tmux load-buffer /tmp/instructions.txt && tmux paste-buffer -t %31
tmux send-keys -t %31 C-m

Never use send-keys directly for prompts or instructions — the text will likely contain characters that trigger tmux modes.

5. Inspect Output (Read Logs)

You can read the output of that pane at any time without switching your context.

Get the current visible screen:

tmux capture-pane -p -t "server-log"

Get the entire history (scrollback):

tmux capture-pane -p -S - -t "server-log"

Use this if the output might have scrolled off the screen.

6. Interact with the Process

If you need to stop or restart the process:

Send Ctrl+C (Interrupt):

tmux send-keys -t "server-log" C-c

Kill the window (Clean up):

tmux kill-window -t "server-log"

7. Advanced: Chaining Commands

You can chain multiple tmux commands in a single invocation using ';' (note the quotes to avoid interpretation by the shell). This is faster and cleaner than running multiple tmux commands.

Example: Create window and start process in one go:

tmux new-window -n "server-log" -d ';' send-keys -t "server-log" "npm start" C-m

Quick Reference

| Task | Command | |------|---------| | Create window | tmux new-window -n "ID" -d | | Run command | tmux send-keys -t "ID" "cmd" C-m | | Send literal text | tmux send-keys -l -t "ID" "text" | | Send multi-line | tmux load-buffer file && tmux paste-buffer -t "ID" | | Read output | tmux capture-pane -p -t "ID" | | Interrupt | tmux send-keys -t "ID" C-c | | Kill window | tmux kill-window -t "ID" |