diff --git a/packages/tui/src/context/keymap.tsx b/packages/tui/src/context/keymap.tsx
index 521b3aeac86..59d245ac263 100644
--- a/packages/tui/src/context/keymap.tsx
+++ b/packages/tui/src/context/keymap.tsx
@@ -78,11 +78,7 @@ function Provider(props: ParentProps<{ config?: KeymapConfig }>) {
const dispose = [
registerCommaBindings(keymap),
keymap.appendBindingExpander((context) => {
- const key = Object.entries({ enter: "return", esc: "escape", pgdown: "pagedown", pgup: "pageup" }).reduce(
- (result, [alias, value]) =>
- result.replace(new RegExp(`(^|[+,\\s>])${alias}(?=$|[+,\\s<])`, "gi"), `$1${value}`),
- context.input,
- )
+ const key = expandKeyAliases(context.input)
if (key === context.input) return
return [{ key, displays: context.displays }]
}),
@@ -134,7 +130,7 @@ function Provider(props: ParentProps<{ config?: KeymapConfig }>) {
].flatMap((command) => config.keybinds.get(command)),
}),
]
- const leader = config.keybinds.get("leader")?.[0]?.key
+ const leader = resolveLeaderKey(config)
if (leader) {
dispose.push(
registerTimedLeader(keymap, {
@@ -186,7 +182,7 @@ export interface Keymap {
function use(): Keymap {
const value = useValue()
const enabled = useInteractivity()
- const leader = value.config.keybinds.get("leader")?.[0]?.key
+ const leader = resolveLeaderKey(value.config)
const isLeader = leader ? value.keymap.createKeyMatcher(leader) : () => false
return {
dispatch(id, input) {
@@ -452,8 +448,26 @@ function createMode(keymap: OpenTuiKeymap) {
}
}
-function formatOptions(config: KeymapConfig) {
+const ALIAS_PATTERNS = Object.entries({
+ cmd: "super",
+ command: "super",
+ enter: "return",
+ esc: "escape",
+ pgdown: "pagedown",
+ pgup: "pageup",
+}).map(([alias, value]) => [new RegExp(`(^|[+,\\s>])${alias}(?=$|[+,\\s<])`, "gi"), `$1${value}`] as const)
+
+function expandKeyAliases(input: string) {
+ return ALIAS_PATTERNS.reduce((result, [pattern, replacement]) => result.replace(pattern, replacement), input)
+}
+
+function resolveLeaderKey(config: KeymapConfig) {
const leader = config.keybinds.get("leader")?.[0]?.key
+ return typeof leader === "string" ? expandKeyAliases(leader) : leader
+}
+
+function formatOptions(config: KeymapConfig) {
+ const leader = resolveLeaderKey(config)
return {
tokenDisplay: {
leader: leader ? (typeof leader === "string" ? leader : stringifyKeyStroke(leader)) : TuiKeybind.LeaderDefault,
diff --git a/packages/tui/test/keymap.test.tsx b/packages/tui/test/keymap.test.tsx
index f07a4beebb2..d16bb105ac0 100644
--- a/packages/tui/test/keymap.test.tsx
+++ b/packages/tui/test/keymap.test.tsx
@@ -1,4 +1,5 @@
/** @jsxImportSource @opentui/solid */
+import { TextareaRenderable } from "@opentui/core"
import { testRender } from "@opentui/solid"
import { expect, test } from "bun:test"
import { ConfigProvider } from "../src/config"
@@ -139,3 +140,109 @@ test("global commands stay reachable when the mode changes", async () => {
app.renderer.destroy()
}
})
+
+test("cmd and command modifier aliases bind to super without swallowing plain c in textarea", async () => {
+ const calls: string[] = []
+ let shortcuts = () => ({ new: [] as readonly string[], list: "" })
+
+ function Harness() {
+ const view = Keymap.useShortcuts()
+ Keymap.createLayer(() => ({
+ commands: [
+ { id: "session.new", run: () => void calls.push("new") },
+ { id: "session.list", run: () => void calls.push("list") },
+ ],
+ }))
+ shortcuts = () => ({
+ new: view.list("session.new"),
+ list: view.get("session.list") ?? "",
+ })
+ return
+ }
+
+ const app = await testRender(
+ () => (
+
+
+
+
+
+ ),
+ { width: 80, height: 24, kittyKeyboard: true },
+ )
+ try {
+ await app.renderOnce()
+ const editor = app.renderer.currentFocusedEditor
+ if (!(editor instanceof TextareaRenderable)) throw new Error("expected focused textarea")
+
+ app.mockInput.pressKey("c")
+ expect(editor.plainText).toBe("c")
+
+ await app.mockInput.typeText("at")
+ expect(editor.plainText).toBe("cat")
+
+ expect(shortcuts()).toEqual({
+ new: ["super+n", "super+t"],
+ list: "super+l",
+ })
+
+ app.mockInput.pressKey("n", { super: true })
+ app.mockInput.pressKey("t", { super: true })
+ app.mockInput.pressKey("l", { super: true })
+ expect(calls).toEqual(["new", "new", "list"])
+ expect(editor.plainText).toBe("cat")
+ } finally {
+ app.renderer.destroy()
+ }
+})
+
+test("cmd modifier alias works as a leader trigger", async () => {
+ const calls: string[] = []
+
+ function Harness() {
+ Keymap.createLayer(() => ({
+ commands: [{ id: "session.compact", run: () => void calls.push("compact") }],
+ }))
+ return
+ }
+
+ const app = await testRender(
+ () => (
+ c",
+ },
+ })}
+ >
+
+
+
+
+ ),
+ { width: 80, height: 24, kittyKeyboard: true },
+ )
+ try {
+ await app.renderOnce()
+ const editor = app.renderer.currentFocusedEditor
+ if (!(editor instanceof TextareaRenderable)) throw new Error("expected focused textarea")
+
+ app.mockInput.pressKey("c")
+ expect(editor.plainText).toBe("c")
+
+ app.mockInput.pressKey("x", { super: true })
+ app.mockInput.pressKey("c")
+ expect(calls).toEqual(["compact"])
+ expect(editor.plainText).toBe("c")
+ } finally {
+ app.renderer.destroy()
+ }
+})