$npx -y skills add obra/superpowers-lab --skill using-tmux-for-interactive-commandsUse when you need to run interactive CLI tools (vim, git rebase -i, Python REPL, etc.) that require real-time input/output - provides tmux-based approach for controlling interactive sessions through detached sessions and send-keys
| 1 | # Using tmux for Interactive Commands |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Interactive CLI tools (vim, interactive git rebase, REPLs, etc.) cannot be controlled through standard bash because they require a real terminal. tmux provides detached sessions that can be controlled programmatically via `send-keys` and `capture-pane`. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | **Use tmux when:** |
| 10 | - Running vim, nano, or other text editors programmatically |
| 11 | - Controlling interactive REPLs (Python, Node, etc.) |
| 12 | - Handling interactive git commands (`git rebase -i`, `git add -p`) |
| 13 | - Working with full-screen terminal apps (htop, etc.) |
| 14 | - Commands that require terminal control codes or readline |
| 15 | |
| 16 | **Don't use for:** |
| 17 | - Simple non-interactive commands (use regular Bash tool) |
| 18 | - Commands that accept input via stdin redirection |
| 19 | - One-shot commands that don't need interaction |
| 20 | |
| 21 | ## Quick Reference |
| 22 | |
| 23 | | Task | Command | |
| 24 | |------|---------| |
| 25 | | Start session | `tmux new-session -d -s <name> <command>` | |
| 26 | | Send input | `tmux send-keys -t <name> 'text' Enter` | |
| 27 | | Capture output | `tmux capture-pane -t <name> -p` | |
| 28 | | Stop session | `tmux kill-session -t <name>` | |
| 29 | | List sessions | `tmux list-sessions` | |
| 30 | |
| 31 | ## Core Pattern |
| 32 | |
| 33 | ### Before (Won't Work) |
| 34 | ```bash |
| 35 | # This hangs because vim expects interactive terminal |
| 36 | bash -c "vim file.txt" |
| 37 | ``` |
| 38 | |
| 39 | ### After (Works) |
| 40 | ```bash |
| 41 | # Create detached tmux session |
| 42 | tmux new-session -d -s edit_session vim file.txt |
| 43 | |
| 44 | # Send commands (Enter, Escape are tmux key names) |
| 45 | tmux send-keys -t edit_session 'i' 'Hello World' Escape ':wq' Enter |
| 46 | |
| 47 | # Capture what's on screen |
| 48 | tmux capture-pane -t edit_session -p |
| 49 | |
| 50 | # Clean up |
| 51 | tmux kill-session -t edit_session |
| 52 | ``` |
| 53 | |
| 54 | ## Implementation |
| 55 | |
| 56 | ### Basic Workflow |
| 57 | |
| 58 | 1. **Create detached session** with the interactive command |
| 59 | 2. **Wait briefly** for initialization (100-500ms depending on command) |
| 60 | 3. **Send input** using `send-keys` (can send special keys like Enter, Escape) |
| 61 | 4. **Capture output** using `capture-pane -p` to see current screen state |
| 62 | 5. **Repeat** steps 3-4 as needed |
| 63 | 6. **Terminate** session when done |
| 64 | |
| 65 | ### Special Keys |
| 66 | |
| 67 | Common tmux key names: |
| 68 | - `Enter` - Return/newline |
| 69 | - `Escape` - ESC key |
| 70 | - `C-c` - Ctrl+C |
| 71 | - `C-x` - Ctrl+X |
| 72 | - `Up`, `Down`, `Left`, `Right` - Arrow keys |
| 73 | - `Space` - Space bar |
| 74 | - `BSpace` - Backspace |
| 75 | |
| 76 | ### Working Directory |
| 77 | |
| 78 | Specify working directory when creating session: |
| 79 | ```bash |
| 80 | tmux new-session -d -s git_session -c /path/to/repo git rebase -i HEAD~3 |
| 81 | ``` |
| 82 | |
| 83 | ### Helper Wrapper |
| 84 | |
| 85 | For easier use, see `/home/jesse/git/interactive-command/tmux-wrapper.sh`: |
| 86 | ```bash |
| 87 | # Start session |
| 88 | /path/to/tmux-wrapper.sh start <session-name> <command> [args...] |
| 89 | |
| 90 | # Send input |
| 91 | /path/to/tmux-wrapper.sh send <session-name> 'text' Enter |
| 92 | |
| 93 | # Capture current state |
| 94 | /path/to/tmux-wrapper.sh capture <session-name> |
| 95 | |
| 96 | # Stop |
| 97 | /path/to/tmux-wrapper.sh stop <session-name> |
| 98 | ``` |
| 99 | |
| 100 | ## Common Patterns |
| 101 | |
| 102 | ### Python REPL |
| 103 | ```bash |
| 104 | tmux new-session -d -s python python3 -i |
| 105 | tmux send-keys -t python 'import math' Enter |
| 106 | tmux send-keys -t python 'print(math.pi)' Enter |
| 107 | tmux capture-pane -t python -p # See output |
| 108 | tmux kill-session -t python |
| 109 | ``` |
| 110 | |
| 111 | ### Vim Editing |
| 112 | ```bash |
| 113 | tmux new-session -d -s vim vim /tmp/file.txt |
| 114 | sleep 0.3 # Wait for vim to start |
| 115 | tmux send-keys -t vim 'i' 'New content' Escape ':wq' Enter |
| 116 | # File is now saved |
| 117 | ``` |
| 118 | |
| 119 | ### Interactive Git Rebase |
| 120 | ```bash |
| 121 | tmux new-session -d -s rebase -c /repo/path git rebase -i HEAD~3 |
| 122 | sleep 0.5 |
| 123 | tmux capture-pane -t rebase -p # See rebase editor |
| 124 | # Send commands to modify rebase instructions |
| 125 | tmux send-keys -t rebase 'Down' 'Home' 'squash' Escape |
| 126 | tmux send-keys -t rebase ':wq' Enter |
| 127 | ``` |
| 128 | |
| 129 | ## Common Mistakes |
| 130 | |
| 131 | ### Not Waiting After Session Start |
| 132 | **Problem:** Capturing immediately after `new-session` shows blank screen |
| 133 | |
| 134 | **Fix:** Add brief sleep (100-500ms) before first capture |
| 135 | ```bash |
| 136 | tmux new-session -d -s sess command |
| 137 | sleep 0.3 # Let command initialize |
| 138 | tmux capture-pane -t sess -p |
| 139 | ``` |
| 140 | |
| 141 | ### Forgetting Enter Key |
| 142 | **Problem:** Commands typed but not executed |
| 143 | |
| 144 | **Fix:** Explicitly send Enter |
| 145 | ```bash |
| 146 | tmux send-keys -t sess 'print("hello")' Enter # Note: Enter is separate argument |
| 147 | ``` |
| 148 | |
| 149 | ### Using Wrong Key Names |
| 150 | **Problem:** `tmux send-keys -t sess '\n'` doesn't work |
| 151 | |
| 152 | **Fix:** Use tmux key names: `Enter`, not `\n` |
| 153 | ```bash |
| 154 | tmux send-keys -t sess 'text' Enter # ✓ |
| 155 | tmux send-keys -t sess 'text\n' # ✗ |
| 156 | ``` |
| 157 | |
| 158 | ### Not Cleaning Up Sessions |
| 159 | **Problem:** Orphaned tmux sessions accumulate |
| 160 | |
| 161 | **Fix:** Always kill sessions when done |
| 162 | ```bash |
| 163 | tmux kill-session -t session_name |
| 164 | # Or check for existing: tmux has-session -t name 2>/dev/null |
| 165 | ``` |
| 166 | |
| 167 | ## Real-World Impact |
| 168 | |
| 169 | - Enables programmatic control of vim/nano for file editing |
| 170 | - Allows automation of interactive git workflows (rebase, add -p) |
| 171 | - Makes REPL-based testing/debugging possible |
| 172 | - Unblocks any tool that requires terminal inter |