Skip to content

Let bound commands through a focused terminal, keeping shell keys - #7888

Merged
ymichael merged 2 commits into
mainfrom
bb/let-bound-commands-through-a-focused-terminal-thr_yvwjtawd4g
Nov 20, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/let-bound-commands-through-a-focused-terminal-thr_yvwjtawd4g

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

With a terminal focused, xterm turns every key it can encode into terminal input, then calls preventDefault and stopPropagation. That covers F-keys, Ctrl+letter, Ctrl+3–8, and on Windows/Linux Alt and Ctrl+Alt chords. Core's command dispatcher listens on window in the bubble phase (AppCommandProvider.tsx), so commands bound to those keys never fired while a terminal had focus, whether core or plugin. The composer already asks the registry first through dispatchAppCommandKey. The terminal never did. (API audit finding C2, kill-test 4.3.)

What changed

The terminal's xterm key handler now asks core's command registry first, except for the keys a shell reads (apps/app/src/components/thread/terminal/terminal-app-command-keys.ts, wired in ThreadTerminalView.tsx after the clipboard handler). The panel terminal and the onboarding terminal both mount through this view. Keyboard dispatch uses core's own dispatchAppCommandKey, so each binding's when context (terminalFocus, modalOpen, …) applies unchanged.

The rule. A focused terminal keeps these keys, even when a command is bound to them:

  • keys without Ctrl, Alt or Cmd (typing, Enter, Escape, Tab, arrows), except F-keys
  • plain Ctrl+letter, plus Ctrl+Space, Ctrl+[, Ctrl+], Ctrl+\ and Ctrl+/ (the C0 control characters)

Every other bound key runs its command from the terminal: F-keys, Ctrl+Shift, Alt and Cmd chords, and Ctrl+digit.

Why all Ctrl+letters. On Windows/Linux, Mod is Ctrl. A shortlist such as C/D/R/L would let core's own Mod+W (close tab), Mod+K, Mod+P, Mod+J and Mod+T take delete-word, kill-line, history, newline and transpose from readline, and Ctrl+B from tmux. The existing test "leaves Ctrl+letter shell keys to the shell on Linux" and the CLI guide already promised this.

Consequence: Ctrl+J stays with the shell. It is a newline (LF), so a command rebound to Ctrl+J still won't fire from a focused terminal. Use a Ctrl+Shift, Alt or Cmd chord, or a function key.

Tradeoff: Alt+letter on Windows/Linux is not reserved. xterm sends it as Meta, so a bound Alt+letter now beats readline's Meta-b/f/d. No core default and no community-plugin default uses plain Alt+letter. Reserving those keys would regress macOS, where Option chords already reach commands.

Community plugins whose defaults now fire with a terminal focused (grep of 503 community plugin repos):

  • nicolay-i/bb-plugin-tabs: F2 rename (all platforms); Mod+Alt+1–9/P/S/W (Windows/Linux)
  • Johannes-Berggren/bb-plugin-jb-flow: nine Alt+Shift+letter commands (Windows/Linux)
  • wy3z/bb-cursor-suite ui-tweaks and vburojevic/bb-plugin-prompts: Alt+digit (Windows/Linux)
  • tarasenko-by/bb-plugin-spaces: Ctrl+Alt+digit (Windows/Linux)
  • kalindudc/bb-marketplace switchboard (Mod+Alt+F/J), chug2k/bb-plugin-retitle (Mod+Alt+R) and jonnik-bb-plugins/bb-plugin-bang-shell (Mod+Alt+Enter), all on Windows/Linux

Unchanged:

  • These defaults are Ctrl+letter on Windows/Linux, so they stay with the shell there as before: Mod+E and Mod+B in architawr's workbench-explorer, Mod+S in sajov's recall, Mod+R in euanguo's nested-sidebar.
  • ekaragodin's terminal-toggle (Ctrl+`) already worked and still does.
  • No markup or attributes changed. [data-app-terminal] stays on the panel section that themes and plugins select.

Docs: docs/configuration.md (Keyboard Shortcuts), the bb guide customization chapter, and the bb-plugin-authoring frontend-renderer-slots reference now state the rule. No plugin SDK or wire change.

How you verified

  • New test in ThreadTerminalView.commands.test.tsx: a key table run against a real xterm, on both macOS and Linux.
    • Bound F5, Alt+Shift+N, Ctrl+Alt+1 and Ctrl+3 must each run their command and send nothing to the shell.
    • Bound Ctrl+J, Ctrl+C, Ctrl+[ and Escape must still reach the shell (\n, \x03, \x1b, \x1b) and run nothing.
    • On main it fails on both platforms at F5 (ran: []). With the fix, all 103 terminal tests pass.
  • Other checks: 120 tests across src/components/commands, composer-commands.test.tsx and app-keybindings.test.ts pass. turbo run typecheck lint --filter=@bb/app and turbo run test --filter=@bb/templates pass.
  • Live check: pnpm start:worktree, headless Chrome, a real zsh running cat -v. sidebar.toggle bound to F5, panel.fullScreen.toggle bound to Ctrl+J.
    • Main: F5 reached the shell as x^[[15~y, the sidebar stayed open, and the window keydown listener never saw F5. Ctrl+J was a newline.
    • Fixed: F5 collapsed the sidebar (aria-expanded true → false), the shell line read xy, and focus stayed in the terminal. Ctrl+J and Ctrl+C still went to the shell.
    • Fixed, with navigator.platform emulated as Linux: Ctrl+K opened no search, Ctrl+W deleted a word in zsh instead of closing the tab, and F5 toggled the sidebar.

AGENT GENERATED

ymichael and others added 2 commits November 19, 2026 20:34
xterm encodes F-keys, Ctrl+digit and (on Windows/Linux) Alt chords as
terminal input and stops their propagation, so core's window keydown
dispatcher never saw them while a terminal had focus. The terminal's key
handler now asks the command registry first, except for the keys a shell
reads: keys without Ctrl/Alt/Cmd and plain Ctrl+letter, Ctrl+Space,
Ctrl+[ ] \ /. Those stay with the shell even when bound, so Mod+K/W/J
on Windows/Linux keep reaching readline.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The app command dispatcher ignores key repeats, so after a bound key ran its
command from a focused terminal, the auto-repeat keydowns fell through to
xterm and reached the shell (holding F5 sent ^[[15~ per repeat). The terminal
handler now swallows repeats of the key whose press ran a command, and
prevents their default so a held F5 cannot reload the page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit ee270f5 into main Nov 20, 2026
52 checks passed
@ymichael
ymichael deleted the bb/let-bound-commands-through-a-focused-terminal-thr_yvwjtawd4g branch November 20, 2026 18:30
lanjak added a commit to lanjak/bb that referenced this pull request Nov 24, 2026
get-bb#7888 added an app command key handler to the terminal that reads the
dispatcher through a ref of the view that created the terminal. A
parked terminal outlives that view, so the handler is wired through the
runtime's live bindings, which each remounted view replaces. This test
fails when the handler keeps the dispatcher ref of the first view.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant