feat(terminal): send text to the Claude pane (:ClaudeCodeSendText) (#197)#272
Merged
Conversation
…ne (#197) Add `require("claudecode.terminal").send_to_terminal(text, opts)` and a `:ClaudeCodeSendText {text}` command that write text into the running Claude terminal's job channel via `chansend`, as if typed at the prompt. A trailing carriage return submits by default; `:ClaudeCodeSendText!` (and `opts.submit = false`) insert without submitting. Multi-line text is sent as a single bracketed-paste block (line endings normalized) so interior newlines don't fire premature submits. Works with the in-editor `native`/`snacks` providers; `external`/`none` run Claude outside Neovim, so there is no pane to write to and the call warns and returns false. The send is gated on `nvim_buf_is_valid` and a resolvable job channel (`b:terminal_job_id` with a `bo.channel` fallback for recovered terminals), and the `chansend` write is honored (closed channel -> false). Verified live against the real Claude CLI (2.1.169): single-line insert+submit, bang insert-only, and multi-line single-submit. 22 new unit tests. Change-Id: I2f68c38c76cf5b07cf6da1ba4c3b553252c5c0fe Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Thomas Kosiewski <tk@coder.com>
Member
Author
|
Codex Review: Didn't find any major issues. Breezy! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a way to send arbitrary text to the currently-open Claude Code terminal pane, as if typed at the prompt — the feature requested in #197 (for scripting and keymaps).
:ClaudeCodeSendText {text}— types{text}into the open Claude terminal and submits it.:ClaudeCodeSendText!(bang) inserts without submitting.require("claudecode.terminal").send_to_terminal(text, opts)— the programmatic primitive (opts.submitdefaulttrue,opts.focusdefaultfalse).It writes to the terminal's job channel with
vim.fn.chansend; a trailing carriage return (\r) is what submits at Claude's prompt. No window focus is required, which is the point for scripting.Closes #197.
How it works
chansendinto a Neovim:terminalwrites to the same PTY master Neovim uses when forwarding a real keypress, sochansend(chan, "\r")is byte-identical to pressing Enter and submits. (This is not affected by anthropics/claude-code#15553, which is about VS Code'sterminal.sendText— a different layer.)Implementation notes:
get_active_terminal_bufnr()→b:terminal_job_id, with abo.channelfallback for a recovered terminal whose module-level job id was lost.nvim_buf_is_valid+ a resolvable, non-zero channel; thechansendwrite result is honored (a closed channel → warn +false), so the boolean return is truthful.ESC[200~ … ESC[201~) and line endings are normalized, so interior newlines/CRs don't fire premature submits; the submit\ris sent after the closing marker. (chansendbypassesvim.paste, so thefix_streamed_pasteshim is irrelevant here.)native/snacks).external/nonerun Claude outside Neovim — there's no pane to write to, so the call warns and returnsfalse(same structural limit asfocus_after_send; see theClaudeCodeSendCompleteevent for focusing an out-of-editor session).Verification
Verified end-to-end against the real Claude CLI (2.1.169) driving the plugin's native terminal in an isolated PTY (cursor never left a separate buffer — no focus needed):
:ClaudeCodeSendText What is 7 times 6? …42:ClaudeCodeSendText! this is a draft …send_to_terminal("…\nWhat is 9 plus 10?")19Tests: 22 new unit tests (
tests/unit/terminal/send_text_spec.lua,tests/unit/claudecode_send_text_command_spec.lua) covering single/multi-line, submit/bang, CRLF + lone-CR normalization, theterminal_job_id→bo.channelfallback (incl. job id0), nil/stale channel,focus, and theexternal/nonewarnings.mise run allis green (608 tests, lint + format clean).Relates to #234 (not closed here)
#234 ("send prompt with selection") builds directly on this primitive, and the content composition works — live testing confirmed that broadcasting the selection's
at_mention(Claude inserts@file#Lx-y) and thenchansend-ing the prompt produces the correct@file#Lx-y <prompt>and Claude reads the file. However, an@-mention in Claude's input box (whether MCP-inserted or typed) absorbs the immediately-following\r, so reliable auto-submit needs a separate, timing-gapped\rafter the prompt is inserted. That extra timing dependency (against an async TUI, plus a dependency on undocumented CLI mention-insertion behavior) is fragile enough that I've kept it out of this PR rather than bundle a flaky auto-submit. This PR provides the clean primitive #234 needs; a focused follow-up can layer the selection+prompt UX (e.g.:ClaudeCodeSendWithPromptviavim.ui.input) on top, with the 2-stage submit and a configurable delay.Notes
:ClaudeCodeSendTextsends to the currently-open pane; it does not auto-launch Claude (open it with:ClaudeCodefirst). This keeps behavior predictable — a deferred send into a just-spawned CLI that isn't input-ready yet would silently drop.🤖 Generated with Claude Code