summaryrefslogtreecommitdiff
path: root/gemfeed/2026-02-02-tmux-popup-editor-for-cursor-agent-prompts.html
blob: e525a93dbeb207057980d80af20df89225335f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>A tmux popup editor for Cursor Agent CLI prompts</title>
<link rel="shortcut icon" type="image/gif" href="/favicon.ico" />
<link rel="stylesheet" href="../style.css" />
<link rel="stylesheet" href="style-override.css" />
</head>
<body>
<p class="header">
<a href="https://foo.zone">Home</a> | <a href="https://codeberg.org/snonux/foo.zone/src/branch/content-md/gemfeed/2026-02-02-tmux-popup-editor-for-cursor-agent-prompts.md">Markdown</a> | <a href="gemini://foo.zone/gemfeed/2026-02-02-tmux-popup-editor-for-cursor-agent-prompts.gmi">Gemini</a>
</p>
<h1 style='display: inline' id='a-tmux-popup-editor-for-cursor-agent-cli-prompts'>A tmux popup editor for Cursor Agent CLI prompts</h1><br />
<br />
<span class='quote'>Published at 2026-02-01T20:24:16+02:00</span><br />
<br />
<span>...and any other TUI based application</span><br />
<br />
<h2 style='display: inline' id='table-of-contents'>Table of Contents</h2><br />
<br />
<ul>
<li><a href='#a-tmux-popup-editor-for-cursor-agent-cli-prompts'>A tmux popup editor for Cursor Agent CLI prompts</a></li>
<li>⇢ <a href='#why-i-built-this'>Why I built this</a></li>
<li>⇢ <a href='#what-it-is'>What it is</a></li>
<li>⇢ <a href='#how-it-works-overview'>How it works (overview)</a></li>
<li>⇢ ⇢ <a href='#workflow-diagram'>Workflow diagram</a></li>
<li>⇢ <a href='#challenges-and-small-discoveries'>Challenges and small discoveries</a></li>
<li>⇢ <a href='#test-cases-for-a-future-rewrite'>Test cases (for a future rewrite)</a></li>
<li>⇢ <a href='#almost-works-with-any-editor-or-any-tui'>(Almost) works with any editor (or any TUI)</a></li>
</ul><br />
<h2 style='display: inline' id='why-i-built-this'>Why I built this</h2><br />
<br />
<span>I spend some time in Cursor Agent (the CLI version of the Cursor IDE, I don&#39;t like really the IDE), and I also jump between Claude Code CLI, Ampcode, Gemini CLI, OpenAI Codex CLI, OpenCode, and Aider just to see how things are evolving. But for the next month I&#39;ll be with Cursor Agent.</span><br />
<br />
<a class='textlink' href='https://cursor.com/cli'>https://cursor.com/cli</a><br />
<br />
<span>Short prompts are fine in the inline input, but for longer prompts I want a real editor: spellcheck, search/replace, multiple cursors, and all the Helix muscle memory I already have.</span><br />
<br />
<span>Cursor Agent has a Vim editing mode, but not Helix. And even in Vim mode I can&#39;t use my full editor setup. I want the real thing, not a partial emulation.</span><br />
<br />
<a class='textlink' href='https://helix-editor.com'>https://helix-editor.com</a><br />
<a class='textlink' href='https://www.vim.org'>https://www.vim.org</a><br />
<a class='textlink' href='https://neovim.io'>https://neovim.io</a><br />
<br />
<span>So I built a tiny tmux popup editor. It opens <span class='inlinecode'>$EDITOR</span> (Helix for me), and when I close it, the buffer is sent back into the prompt. It sounds simple, but it feels surprisingly native.</span><br />
<br />
<span>This is how it looks like:</span><br />
<br />
<a href='./tmux-popup-editor-for-cursor-agent-prompts/demo1.png'><img alt='Popup editor in action' title='Popup editor in action' src='./tmux-popup-editor-for-cursor-agent-prompts/demo1.png' /></a><br />
<br />
<h2 style='display: inline' id='what-it-is'>What it is</h2><br />
<br />
<span>The idea is straightforward:</span><br />
<br />
<ul>
<li>A tmux key binding <span class='inlinecode'>prefix-e</span> opens a popup overlay near the bottom of the screen.</li>
<li>The popup starts <span class='inlinecode'>$EDITOR</span> on a temp file.</li>
<li>When I exit the editor, the script sends the contents back to the original pane with <span class='inlinecode'>tmux send-keys</span>.</li>
</ul><br />
<span>It also pre-fills the temp file with whatever is already typed after Cursor Agent&#39;s <span class='inlinecode'>→</span> prompt, so I can continue where I left off.</span><br />
<br />
<h2 style='display: inline' id='how-it-works-overview'>How it works (overview)</h2><br />
<br />
<span>This is the tmux binding I use (trimmed to the essentials):</span><br />
<br />
<pre>
bind-key e run-shell -b "tmux display-message -p &#39;#{pane_id}&#39;
  &gt; /tmp/tmux-edit-target-#{client_pid} \;
  tmux popup -E -w 90% -h 35% -x 5% -y 65% -d &#39;#{pane_current_path}&#39;
  \"~/scripts/tmux-edit-send /tmp/tmux-edit-target-#{client_pid}\""
</pre>
<br />
<h3 style='display: inline' id='workflow-diagram'>Workflow diagram</h3><br />
<br />
<span>This is the whole workflow:</span><br />
<br />
<pre>
┌────────────────────┐   ┌───────────────┐   ┌─────────────────────┐   ┌─────────────────────┐
│ Cursor input box   │--&gt;| tmux keybind  │--&gt;| popup runs script   │--&gt;| capture + prefill   │
│ (prompt pane)      │   │ prefix + e    │   │ tmux-edit-send      │   │ temp file           │
└────────────────────┘   └───────────────┘   └─────────────────────┘   └─────────────────────┘
                                                                                 |
                                                                                 v
┌────────────────────┐   ┌────────────────────┐   ┌────────────────────┐   ┌────────────────────┐
│ Cursor input box   │&lt;--| send-keys back     |&lt;--| close editor+popup |&lt;--| edit temp file     |
│ (prompt pane)      │   │ to original pane   │   │ (exit $EDITOR)     │   │ in $EDITOR         │
└────────────────────┘   └────────────────────┘   └────────────────────┘   └────────────────────┘
</pre>
<br />
<span>And this is how it looks like after sending back the text to the Cursor Agent&#39;s input:</span><br />
<br />
<a href='./tmux-popup-editor-for-cursor-agent-prompts/demo2.png'><img alt='Prefilled prompt text' title='Prefilled prompt text' src='./tmux-popup-editor-for-cursor-agent-prompts/demo2.png' /></a><br />
<br />
<span>And here is the full script. It is a bit ugly since it&#39;s shell (written with Cursor Agent with GPT-5.2-Codex), and I might (let) rewrite it in Go with propper unit tests, config-file, multi-agent support and release it once I have time. But it works well enough for now.</span><br />
<br />
<span class='quote'>Update 2026-02-08: This functionality has been integrated into the hexai project (https://codeberg.org/snonux/hexai) with proper multi-agent support for Cursor Agent, Claude Code CLI, and Ampcode. The hexai version includes unit tests, configuration files, and better agent detection. While still experimental, it&#39;s more robust than this shell script. See the hexai-tmux-edit command for details.</span><br />
<br />
<a class='textlink' href='https://codeberg.org/snonux/hexai'>https://codeberg.org/snonux/hexai</a><br />
<br />
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><i><font color="silver">#!/usr/bin/env bash</font></i>
<b><u><font color="#000000">set</font></u></b> -u -o pipefail

LOG_ENABLED=<font color="#000000">0</font>
log_file=<font color="#808080">"${TMPDIR:-/tmp}/tmux-edit-send.log"</font>
log() {
  <b><u><font color="#000000">if</font></u></b> [ <font color="#808080">"$LOG_ENABLED"</font> -eq <font color="#000000">1</font> ]; <b><u><font color="#000000">then</font></u></b>
    <b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s</font>\n<font color="#808080">'</font> <font color="#808080">"$*"</font> &gt;&gt; <font color="#808080">"$log_file"</font>
  <b><u><font color="#000000">fi</font></u></b>
}

<i><font color="silver"># Read the target pane id from a temp file created by tmux binding.</font></i>
read_target_from_file() {
  <b><u><font color="#000000">local</font></u></b> file_path=<font color="#808080">"$1"</font>
  <b><u><font color="#000000">local</font></u></b> pane_id
  <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$file_path"</font> ] &amp;&amp; [ -f <font color="#808080">"$file_path"</font> ]; <b><u><font color="#000000">then</font></u></b>
    pane_id=<font color="#808080">"$(sed -n '1p' "</font>$file_path<font color="#808080">" | tr -d '[:space:]')"</font>
    <i><font color="silver"># Ensure pane ID has % prefix</font></i>
    <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$pane_id"</font> ] &amp;&amp; [[ <font color="#808080">"$pane_id"</font> != %* ]]; <b><u><font color="#000000">then</font></u></b>
      pane_id=<font color="#808080">"%${pane_id}"</font>
    <b><u><font color="#000000">fi</font></u></b>
    <b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s'</font> <font color="#808080">"$pane_id"</font>
  <b><u><font color="#000000">fi</font></u></b>
}

<i><font color="silver"># Read the target pane id from tmux environment if present.</font></i>
read_target_from_env() {
  <b><u><font color="#000000">local</font></u></b> env_line pane_id
  env_line=<font color="#808080">"$(tmux show-environment -g TMUX_EDIT_TARGET 2&gt;/dev/null || true)"</font>
  <b><u><font color="#000000">case</font></u></b> <font color="#808080">"$env_line"</font> <b><u><font color="#000000">in</font></u></b>
    TMUX_EDIT_TARGET=*)
      pane_id=<font color="#808080">"${env_line#TMUX_EDIT_TARGET=}"</font>
      <i><font color="silver"># Ensure pane ID has % prefix</font></i>
      <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$pane_id"</font> ] &amp;&amp; [[ <font color="#808080">"$pane_id"</font> != %* ]] &amp;&amp; [[ <font color="#808080">"$pane_id"</font> =~ ^[<font color="#000000">0</font>-<font color="#000000">9</font>]+$ ]]; <b><u><font color="#000000">then</font></u></b>
        pane_id=<font color="#808080">"%${pane_id}"</font>
      <b><u><font color="#000000">fi</font></u></b>
      <b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s'</font> <font color="#808080">"$pane_id"</font>
      ;;
  <b><u><font color="#000000">esac</font></u></b>
}

<i><font color="silver"># Resolve the target pane id, falling back to the last pane.</font></i>
resolve_target_pane() {
  <b><u><font color="#000000">local</font></u></b> candidate=<font color="#808080">"$1"</font>
  <b><u><font color="#000000">local</font></u></b> current_pane last_pane

  current_pane=<font color="#808080">"$(tmux display-message -p "</font><i><font color="silver">#{pane_id}" 2&gt;/dev/null || true)"</font></i>
  log <font color="#808080">"current pane=${current_pane:-&lt;empty&gt;}"</font>
  
  <i><font color="silver"># Ensure candidate has % prefix if it's a pane ID</font></i>
  <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$candidate"</font> ] &amp;&amp; [[ <font color="#808080">"$candidate"</font> =~ ^[<font color="#000000">0</font>-<font color="#000000">9</font>]+$ ]]; <b><u><font color="#000000">then</font></u></b>
    candidate=<font color="#808080">"%${candidate}"</font>
    log <font color="#808080">"normalized candidate to $candidate"</font>
  <b><u><font color="#000000">fi</font></u></b>
  
  <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$candidate"</font> ] &amp;&amp; [[ <font color="#808080">"$candidate"</font> == *<font color="#808080">"#{"</font>* ]]; <b><u><font color="#000000">then</font></u></b>
    log <font color="#808080">"format target detected, clearing"</font>
    candidate=<font color="#808080">""</font>
  <b><u><font color="#000000">fi</font></u></b>
  <b><u><font color="#000000">if</font></u></b> [ -z <font color="#808080">"$candidate"</font> ]; <b><u><font color="#000000">then</font></u></b>
    candidate=<font color="#808080">"$(tmux display-message -p "</font><i><font color="silver">#{last_pane}" 2&gt;/dev/null || true)"</font></i>
    log <font color="#808080">"using last pane as fallback: $candidate"</font>
  <b><u><font color="#000000">elif</font></u></b> [ <font color="#808080">"$candidate"</font> = <font color="#808080">"$current_pane"</font> ]; <b><u><font color="#000000">then</font></u></b>
    last_pane=<font color="#808080">"$(tmux display-message -p "</font><i><font color="silver">#{last_pane}" 2&gt;/dev/null || true)"</font></i>
    <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$last_pane"</font> ]; <b><u><font color="#000000">then</font></u></b>
      candidate=<font color="#808080">"$last_pane"</font>
      log <font color="#808080">"candidate was current, using last pane: $candidate"</font>
    <b><u><font color="#000000">fi</font></u></b>
  <b><u><font color="#000000">fi</font></u></b>
  <b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s'</font> <font color="#808080">"$candidate"</font>
}

<i><font color="silver"># Capture the latest multi-line prompt content from the pane.</font></i>
capture_prompt_text() {
  <b><u><font color="#000000">local</font></u></b> target=<font color="#808080">"$1"</font>
  tmux capture-pane -p -t <font color="#808080">"$target"</font> -S -<font color="#000000">2000</font> <font color="#000000">2</font>&gt;/dev/null | awk <font color="#808080">'</font>
<font color="#808080">    function trim_box(line) {</font>
<font color="#808080">      sub(/^ *│ ?/, "", line)</font>
<font color="#808080">      sub(/ *│ *$/, "", line)</font>
<font color="#808080">      sub(/[[:space:]]+$/, "", line)</font>
<font color="#808080">      return line</font>
<font color="#808080">    }</font>
<font color="#808080">    /^ *│ *→/ &amp;&amp; index($0,"INSERT")==0 &amp;&amp; index($0,"Add a follow-up")==0 {</font>
<font color="#808080">      if (text != "") last = text</font>
<font color="#808080">      text = ""</font>
<font color="#808080">      capture = 1</font>
<font color="#808080">      line = $0</font>
<font color="#808080">      sub(/^.*→ ?/, "", line)</font>
<font color="#808080">      line = trim_box(line)</font>
<font color="#808080">      if (line != "") text = line</font>
<font color="#808080">      next</font>
<font color="#808080">    }</font>
<font color="#808080">    capture {</font>
<font color="#808080">      if ($0 ~ /^ *└/) {</font>
<font color="#808080">        capture = 0</font>
<font color="#808080">        if (text != "") last = text</font>
<font color="#808080">        next</font>
<font color="#808080">      }</font>
<font color="#808080">      if ($0 ~ /^ *│/ &amp;&amp; index($0,"INSERT")==0 &amp;&amp; index($0,"Add a follow-up")==0) {</font>
<font color="#808080">        line = trim_box($0)</font>
<font color="#808080">        if (line != "") {</font>
<font color="#808080">          if (text != "") text = text " " line</font>
<font color="#808080">          else text = line</font>
<font color="#808080">        }</font>
<font color="#808080">      }</font>
<font color="#808080">    }</font>
<font color="#808080">    END {</font>
<font color="#808080">      if (text != "") last = text</font>
<font color="#808080">      if (last != "") print last</font>
<font color="#808080">    }</font>
<font color="#808080">  '</font>
}

<i><font color="silver"># Write captured prompt text into the temp file if available.</font></i>
prefill_tmpfile() {
  <b><u><font color="#000000">local</font></u></b> tmpfile=<font color="#808080">"$1"</font>
  <b><u><font color="#000000">local</font></u></b> prompt_text=<font color="#808080">"$2"</font>
  <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$prompt_text"</font> ]; <b><u><font color="#000000">then</font></u></b>
    <b><u><font color="#000000">printf</font></u></b> <font color="#808080">'%s</font>\n<font color="#808080">'</font> <font color="#808080">"$prompt_text"</font> &gt; <font color="#808080">"$tmpfile"</font>
  <b><u><font color="#000000">fi</font></u></b>
}

<i><font color="silver"># Ensure the target pane exists before sending keys.</font></i>
validate_target_pane() {
  <b><u><font color="#000000">local</font></u></b> target=<font color="#808080">"$1"</font>
  <b><u><font color="#000000">local</font></u></b> pane target_found
  <b><u><font color="#000000">if</font></u></b> [ -z <font color="#808080">"$target"</font> ]; <b><u><font color="#000000">then</font></u></b>
    log <font color="#808080">"error: no target pane determined"</font>
    echo <font color="#808080">"Could not determine target pane."</font> &gt;&amp;<font color="#000000">2</font>
    <b><u><font color="#000000">return</font></u></b> <font color="#000000">1</font>
  <b><u><font color="#000000">fi</font></u></b>
  target_found=<font color="#000000">0</font>
  log <font color="#808080">"validate: looking for target='$target' in all panes:"</font>
  <b><u><font color="#000000">for</font></u></b> pane <b><u><font color="#000000">in</font></u></b> $(tmux list-panes -a -F <font color="#808080">"#{pane_id}"</font> <font color="#000000">2</font>&gt;/dev/null || <b><u><font color="#000000">true</font></u></b>); <b><u><font color="#000000">do</font></u></b>
    log <font color="#808080">"validate: checking pane='$pane'"</font>
    <b><u><font color="#000000">if</font></u></b> [ <font color="#808080">"$pane"</font> = <font color="#808080">"$target"</font> ]; <b><u><font color="#000000">then</font></u></b>
      target_found=<font color="#000000">1</font>
      log <font color="#808080">"validate: MATCH FOUND!"</font>
      <b><u><font color="#000000">break</font></u></b>
    <b><u><font color="#000000">fi</font></u></b>
  <b><u><font color="#000000">done</font></u></b>
  <b><u><font color="#000000">if</font></u></b> [ <font color="#808080">"$target_found"</font> -ne <font color="#000000">1</font> ]; <b><u><font color="#000000">then</font></u></b>
    log <font color="#808080">"error: target pane not found: $target"</font>
    echo <font color="#808080">"Target pane not found: $target"</font> &gt;&amp;<font color="#000000">2</font>
    <b><u><font color="#000000">return</font></u></b> <font color="#000000">1</font>
  <b><u><font color="#000000">fi</font></u></b>
  log <font color="#808080">"validate: target pane validated successfully"</font>
}

<i><font color="silver"># Send temp file contents to the target pane line by line.</font></i>
send_content() {
  <b><u><font color="#000000">local</font></u></b> target=<font color="#808080">"$1"</font>
  <b><u><font color="#000000">local</font></u></b> tmpfile=<font color="#808080">"$2"</font>
  <b><u><font color="#000000">local</font></u></b> prompt_text=<font color="#808080">"$3"</font>
  <b><u><font color="#000000">local</font></u></b> first_line=<font color="#000000">1</font>
  <b><u><font color="#000000">local</font></u></b> line
  log <font color="#808080">"send_content: target=$target, prompt_text='$prompt_text'"</font>
  <b><u><font color="#000000">while</font></u></b> IFS= <b><u><font color="#000000">read</font></u></b> -r line || [ -n <font color="#808080">"$line"</font> ]; <b><u><font color="#000000">do</font></u></b>
    log <font color="#808080">"send_content: read line='$line'"</font>
    <b><u><font color="#000000">if</font></u></b> [ <font color="#808080">"$first_line"</font> -eq <font color="#000000">1</font> ] &amp;&amp; [ -n <font color="#808080">"$prompt_text"</font> ]; <b><u><font color="#000000">then</font></u></b>
      <b><u><font color="#000000">if</font></u></b> [[ <font color="#808080">"$line"</font> == <font color="#808080">"$prompt_text"</font>* ]]; <b><u><font color="#000000">then</font></u></b>
        <b><u><font color="#000000">local</font></u></b> old_line=<font color="#808080">"$line"</font>
        line=<font color="#808080">"${line#"</font>$prompt_text<font color="#808080">"}"</font>
        log <font color="#808080">"send_content: stripped prompt, was='$old_line' now='$line'"</font>
      <b><u><font color="#000000">fi</font></u></b>
    <b><u><font color="#000000">fi</font></u></b>
    first_line=<font color="#000000">0</font>
    log <font color="#808080">"send_content: sending line='$line'"</font>
    tmux send-keys -t <font color="#808080">"$target"</font> -l <font color="#808080">"$line"</font>
    tmux send-keys -t <font color="#808080">"$target"</font> Enter
  <b><u><font color="#000000">done</font></u></b> &lt; <font color="#808080">"$tmpfile"</font>
  log <font color="#808080">"sent content to $target"</font>
}

<i><font color="silver"># Main entry point.</font></i>
main() {
  <b><u><font color="#000000">local</font></u></b> target_file=<font color="#808080">"${1:-}"</font>
  <b><u><font color="#000000">local</font></u></b> target
  <b><u><font color="#000000">local</font></u></b> editor=<font color="#808080">"${EDITOR:-vi}"</font>
  <b><u><font color="#000000">local</font></u></b> tmpfile
  <b><u><font color="#000000">local</font></u></b> prompt_text

  log <font color="#808080">"=== tmux-edit-send starting ==="</font>
  log <font color="#808080">"target_file=$target_file"</font>
  log <font color="#808080">"EDITOR=$editor"</font>
  
  target=<font color="#808080">"$(read_target_from_file "</font>$target_file<font color="#808080">" || true)"</font>
  <b><u><font color="#000000">if</font></u></b> [ -n <font color="#808080">"$target"</font> ]; <b><u><font color="#000000">then</font></u></b>
    log <font color="#808080">"file target=${target:-&lt;empty&gt;}"</font>
    rm -f <font color="#808080">"$target_file"</font>
  <b><u><font color="#000000">fi</font></u></b>
  <b><u><font color="#000000">if</font></u></b> [ -z <font color="#808080">"$target"</font> ]; <b><u><font color="#000000">then</font></u></b>
    target=<font color="#808080">"${TMUX_EDIT_TARGET:-}"</font>
  <b><u><font color="#000000">fi</font></u></b>
  log <font color="#808080">"env target=${target:-&lt;empty&gt;}"</font>
  <b><u><font color="#000000">if</font></u></b> [ -z <font color="#808080">"$target"</font> ]; <b><u><font color="#000000">then</font></u></b>
    target=<font color="#808080">"$(read_target_from_env || true)"</font>
  <b><u><font color="#000000">fi</font></u></b>
  log <font color="#808080">"tmux env target=${target:-&lt;empty&gt;}"</font>
  target=<font color="#808080">"$(resolve_target_pane "</font>$target<font color="#808080">")"</font>
  log <font color="#808080">"fallback target=${target:-&lt;empty&gt;}"</font>

  tmpfile=<font color="#808080">"$(mktemp)"</font>
  log <font color="#808080">"created tmpfile=$tmpfile"</font>
  <b><u><font color="#000000">if</font></u></b> [ ! -f <font color="#808080">"$tmpfile"</font> ]; <b><u><font color="#000000">then</font></u></b>
    log <font color="#808080">"ERROR: mktemp failed to create file"</font>
    echo <font color="#808080">"ERROR: mktemp failed"</font> &gt;&amp;<font color="#000000">2</font>
    <b><u><font color="#000000">exit</font></u></b> <font color="#000000">1</font>
  <b><u><font color="#000000">fi</font></u></b>
  mv <font color="#808080">"$tmpfile"</font> <font color="#808080">"${tmpfile}.md"</font> <font color="#000000">2</font>&gt;&amp;<font color="#000000">1</font> | <b><u><font color="#000000">while</font></u></b> <b><u><font color="#000000">read</font></u></b> -r line; <b><u><font color="#000000">do</font></u></b> log <font color="#808080">"mv output: $line"</font>; <b><u><font color="#000000">done</font></u></b>
  tmpfile=<font color="#808080">"${tmpfile}.md"</font>
  log <font color="#808080">"renamed to tmpfile=$tmpfile"</font>
  <b><u><font color="#000000">if</font></u></b> [ ! -f <font color="#808080">"$tmpfile"</font> ]; <b><u><font color="#000000">then</font></u></b>
    log <font color="#808080">"ERROR: tmpfile does not exist after rename"</font>
    echo <font color="#808080">"ERROR: tmpfile rename failed"</font> &gt;&amp;<font color="#000000">2</font>
    <b><u><font color="#000000">exit</font></u></b> <font color="#000000">1</font>
  <b><u><font color="#000000">fi</font></u></b>
  <b><u><font color="#000000">trap</font></u></b> <font color="#808080">'rm -f "$tmpfile"'</font> EXIT

  log <font color="#808080">"capturing prompt text from target=$target"</font>
  prompt_text=<font color="#808080">"$(capture_prompt_text "</font>$target<font color="#808080">")"</font>
  log <font color="#808080">"captured prompt_text='$prompt_text'"</font>
  prefill_tmpfile <font color="#808080">"$tmpfile"</font> <font color="#808080">"$prompt_text"</font>
  log <font color="#808080">"prefilled tmpfile"</font>

  log <font color="#808080">"launching editor: $editor $tmpfile"</font>
  <font color="#808080">"$editor"</font> <font color="#808080">"$tmpfile"</font>
  <b><u><font color="#000000">local</font></u></b> editor_exit=$?
  log <font color="#808080">"editor exited with status $editor_exit"</font>

  <b><u><font color="#000000">if</font></u></b> [ ! -s <font color="#808080">"$tmpfile"</font> ]; <b><u><font color="#000000">then</font></u></b>
    log <font color="#808080">"empty file, nothing sent"</font>
    <b><u><font color="#000000">exit</font></u></b> <font color="#000000">0</font>
  <b><u><font color="#000000">fi</font></u></b>
  
  log <font color="#808080">"tmpfile contents:"</font>
  log <font color="#808080">"$(cat "</font>$tmpfile<font color="#808080">")"</font>

  log <font color="#808080">"validating target pane"</font>
  validate_target_pane <font color="#808080">"$target"</font>
  log <font color="#808080">"sending content to target=$target"</font>
  send_content <font color="#808080">"$target"</font> <font color="#808080">"$tmpfile"</font> <font color="#808080">"$prompt_text"</font>
  log <font color="#808080">"=== tmux-edit-send completed ==="</font>
}

main <font color="#808080">"$@"</font>
</pre>
<br />
<h2 style='display: inline' id='challenges-and-small-discoveries'>Challenges and small discoveries</h2><br />
<br />
<span>The problems were mostly small but annoying:</span><br />
<br />
<ul>
<li>Getting the right target pane was the first hurdle. I ended up storing the pane id in a file because of tmux format expansion quirks.</li>
<li>The Cursor UI draws a nice box around the prompt, so the prompt line contains a <span class='inlinecode'>│</span> and other markers. I had to filter those out and strip the box-drawing characters.</li>
<li>When I prefilled text and then sent it back, I sometimes duplicated the prompt. Stripping the prefilled prompt text from the submitted text fixed that.</li>
</ul><br />
<h2 style='display: inline' id='test-cases-for-a-future-rewrite'>Test cases (for a future rewrite)</h2><br />
<br />
<span>These are the cases I test whenever I touch the script:</span><br />
<br />
<ul>
<li>Single-line prompt: capture everything after <span class='inlinecode'>→</span> and prefill the editor.</li>
<li>Multi-line boxed prompt: capture the wrapped lines inside the <span class='inlinecode'>│ ... │</span> box and join them with spaces (no newline in the editor).</li>
<li>Ignore UI noise: do not capture lines containing <span class='inlinecode'>INSERT</span> or <span class='inlinecode'>Add a follow-up</span>.</li>
<li>Preserve appended text: if I add <span class='inlinecode'> juju</span> to an existing line, the space before <span class='inlinecode'>juju</span> must survive.</li>
<li>No duplicate send: if the prefilled text is still at the start of the first line, it must be stripped once before sending back.</li>
</ul><br />
<h2 style='display: inline' id='almost-works-with-any-editor-or-any-tui'>(Almost) works with any editor (or any TUI)</h2><br />
<br />
<span>Although I use Helix, this is just <span class='inlinecode'>$EDITOR</span>. If you prefer Vim, Neovim, or something more exotic, it should work. The same mechanism can be used to feed text into any TUI that reads from a terminal pane, not just Cursor Agent.</span><br />
<br />
<span>One caveat: different agents draw different prompt UIs, so the capture logic depends on the prompt shape. A future version of this script should be more modular in that respect; for now this is just a PoC tailored to Cursor Agent.</span><br />
<br />
<span>Another thing is, what if Cursor decides to change the design of its TUI? I would need to change my script as well.</span><br />
<br />
<span>If I get a chance, I&#39;ll clean it up and rewrite it in Go (and release it properly or include it into Hexai, another AI related tool of mine, of which I haven&#39;t blogged about yet). For now, I am happy with this little hack. It already feels like a native editing workflow for Cursor Agent prompts.</span><br />
<br />
<a class='textlink' href='https://codeberg.org/snonux/hexai'>https://codeberg.org/snonux/hexai</a><br />
<br />
<span>E-Mail your comments to <span class='inlinecode'>paul@nospam.buetow.org</span> :-)</span><br />
<br />
<span>Other related posts are:</span><br />
<br />
<a class='textlink' href='./2026-02-02-tmux-popup-editor-for-cursor-agent-prompts.html'>2026-02-02 A tmux popup editor for Cursor Agent CLI prompts (You are currently reading this)</a><br />
<a class='textlink' href='./2025-08-05-local-coding-llm-with-ollama.html'>2025-08-05 Local LLM for Coding with Ollama on macOS</a><br />
<a class='textlink' href='./2025-05-02-terminal-multiplexing-with-tmux-fish-edition.html'>2025-05-02 Terminal multiplexing with <span class='inlinecode'>tmux</span> - Fish edition</a><br />
<a class='textlink' href='./2024-06-23-terminal-multiplexing-with-tmux.html'>2024-06-23 Terminal multiplexing with <span class='inlinecode'>tmux</span> - Z-Shell edition</a><br />
<br />
<a class='textlink' href='../'>Back to the main site</a><br />
<p class="footer">
	Generated with <a href="https://codeberg.org/snonux/gemtexter">Gemtexter 3.0.1-develop</a> |
	served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/relayd.8">relayd(8)</a>+<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> |
	<a href="https://foo.zone/site-mirrors.html">Site Mirrors</a>
	<br />
	Webring: <a href="https://shring.sh/foo.zone/previous">previous</a> | <a href="https://shring.sh">shring</a> | <a href="https://shring.sh/foo.zone/next">next</a>
</p>
</body>
</html>