Skip to content

Commit 14818af

Browse files
author
JosXa
committed
feat(tui): add Ctrl+Q hotkey to quote selected text into prompt
Adds a configurable keybind (input_quote_selection, default Ctrl+Q) that inserts the current terminal selection into the prompt as a blockquote. Each line is prefixed with '> ' and the block ends with a blank line. Closes anomalyco#21025
1 parent 00fa68b commit 14818af

6 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎packages/opencode/src/cli/cmd/tui/app.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ function App(props: { onSnapshot?: () => Promise<string[]> }) {
302302
const sel = renderer.getSelection()
303303
if (!sel) return
304304

305+
if (keybind.match("input_quote_selection", evt)) {
306+
if (route.data.type !== "session") return
307+
if (!Selection.text(renderer)) return
308+
evt.preventDefault()
309+
evt.stopPropagation()
310+
promptRef.current?.focus()
311+
command.trigger("prompt.quote_selection")
312+
return
313+
}
314+
305315
// Windows Terminal-like behavior:
306316
// - Ctrl+C copies and dismisses selection
307317
// - Esc dismisses selection

‎packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { useKeyboard, useRenderer, type JSX } from "@opentui/solid"
2222
import { Editor } from "@tui/util/editor"
2323
import { useExit } from "../../context/exit"
2424
import { Clipboard } from "../../util/clipboard"
25+
import { Selection } from "../../util/selection"
2526
import type { AssistantMessage, FilePart } from "@opencode-ai/sdk/v2"
2627
import { TuiEvent } from "../../event"
2728
import { iife } from "@/util/iife"
@@ -246,6 +247,19 @@ export function Prompt(props: PromptProps) {
246247
}
247248
},
248249
},
250+
{
251+
title: "Quote selection",
252+
value: "prompt.quote_selection",
253+
keybind: "input_quote_selection",
254+
category: "Prompt",
255+
hidden: true,
256+
onSelect: () => {
257+
const text = Selection.text(renderer)
258+
if (!text) return
259+
insert(Selection.quote(text))
260+
renderer.clearSelection()
261+
},
262+
},
249263
{
250264
title: "Interrupt session",
251265
value: "session.interrupt",
@@ -775,6 +789,10 @@ export function Prompt(props: PromptProps) {
775789
)
776790
}
777791

792+
function insert(text: string) {
793+
input.insertText(text)
794+
}
795+
778796
async function pasteImage(file: { filename?: string; content: string; mime: string }) {
779797
const currentOffset = input.visualCursor.offset
780798
const extmarkStart = currentOffset

‎packages/opencode/src/cli/cmd/tui/util/selection.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ type Renderer = {
1111
}
1212

1313
export namespace Selection {
14+
export function text(renderer: Renderer) {
15+
return renderer.getSelection()?.getSelectedText()
16+
}
17+
18+
export function quote(text: string) {
19+
return text
20+
.replace(/\r\n/g, "\n")
21+
.replace(/\r/g, "\n")
22+
.split("\n")
23+
.map((line) => `> ${line}`)
24+
.join("\n") + "\n\n"
25+
}
26+
1427
export function copy(renderer: Renderer, toast: Toast): boolean {
15-
const text = renderer.getSelection()?.getSelectedText()
28+
const text = Selection.text(renderer)
1629
if (!text) return false
1730

1831
Clipboard.copy(text)

‎packages/opencode/src/config/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ export namespace Config {
670670
variant_cycle: z.string().optional().default("ctrl+t").describe("Cycle model variants"),
671671
input_clear: z.string().optional().default("ctrl+c").describe("Clear input field"),
672672
input_paste: z.string().optional().default("ctrl+v").describe("Paste from clipboard"),
673+
input_quote_selection: z.string().optional().default("ctrl+q").describe("Quote selected text into input"),
673674
input_submit: z.string().optional().default("return").describe("Submit input"),
674675
input_newline: z
675676
.string()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { Selection } from "../../../src/cli/cmd/tui/util/selection"
3+
4+
describe("Selection.quote", () => {
5+
test("quotes each line and appends spacing", () => {
6+
expect(Selection.quote("original\nfew\nlines")).toBe("> original\n> few\n> lines\n\n")
7+
})
8+
9+
test("normalizes windows line endings", () => {
10+
expect(Selection.quote("first\r\nsecond\rthird")).toBe("> first\n> second\n> third\n\n")
11+
})
12+
13+
test("preserves blank lines inside selection", () => {
14+
expect(Selection.quote("first\n\nthird")).toBe("> first\n> \n> third\n\n")
15+
})
16+
})

‎packages/opencode/test/config/tui.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,22 @@ test("migrates tui-specific keys from opencode.json when tui.json does not exist
159159
})
160160
})
161161

162+
test("accepts input_quote_selection keybind", async () => {
163+
await using tmp = await tmpdir({
164+
init: async (dir) => {
165+
await Bun.write(path.join(dir, "tui.json"), JSON.stringify({ keybinds: { input_quote_selection: "ctrl+q" } }, null, 2))
166+
},
167+
})
168+
169+
await Instance.provide({
170+
directory: tmp.path,
171+
fn: async () => {
172+
const config = await TuiConfig.get()
173+
expect(config.keybinds?.input_quote_selection).toBe("ctrl+q")
174+
},
175+
})
176+
})
177+
162178
test("migrates project legacy tui keys even when global tui.json already exists", async () => {
163179
await using tmp = await tmpdir({
164180
init: async (dir) => {

0 commit comments

Comments
 (0)