Skip to content

feat: Navigate prompts with ctrl-p and ctrl-n - #605

Merged
razor-x merged 4 commits into
mainfrom
claude/ctrl-pn-arrow-keys-gmyet0
Aug 4, 2026
Merged

feat: Navigate prompts with ctrl-p and ctrl-n#605
razor-x merged 4 commits into
mainfrom
claude/ctrl-pn-arrow-keys-gmyet0

Conversation

@razor-x

@razor-x razor-x commented Aug 4, 2026

Copy link
Copy Markdown
Member

Summary

Adds Emacs-style navigation to interactive prompts: ctrl-p moves up and ctrl-n moves down, in every prompt kind.

Net change is two files: a ~40-line keypress listener in src/lib/util/prompt.ts and its tests.

Implementation

A single keypress listener on process.stdin re-emits ctrl-p and ctrl-n as synthetic arrow keypresses. Clack navigates on the readline key name, so one synthetic arrow key works across select, autocomplete, multiselect, and confirm prompts alike.

Clack's own alias table can't express this: aliases match bare key names with no awareness of ctrl, so aliasing p/n would hijack the plain letters, and aliases are ignored entirely by prompts that track typed input — including the autocomplete prompts this CLI uses most. Note clack already supports vim's h/j/k/l letters in select prompts out of the box; this only adds the control-key variants.

The listener is installed once, lazily, from ensureInteractive(). Keypress events only flow while a prompt holds stdin in raw mode, so it is inert the rest of the time and never holds the process open. The original ctrl-p/n events still reach clack but are no-ops there, since readline treats them as history navigation and a prompt has no history.

Scope

An earlier revision of this branch also added ctrl-j/ctrl-k, right-arrow-submits, and left-arrow-goes-back. That needed a custom input stream in front of clack's readline — ctrl-j arrives as \x0a, which readline consumes as a line submit and which wipes the typed autocomplete filter, so suppressing it requires sitting on the byte stream rather than listening for decoded keypresses. That approach also had to mirror whether the input was empty, duplicating state clack already tracks.

That revision has been reverted (commit 5adf0a4) to keep this PR to the small, clean win. Back-navigation and the remaining control keys are better served by a keymap option upstream in clack, and will come as a separate change.

Testing

src/lib/util/prompt.test.ts covers the key mapping — ctrl-p/n map to up/down, and plain p/n, ctrl-c, meta/shift combinations, and already-arrow keys are all left alone — plus the re-emit behavior on a stream.

Full suite green: 174 tests across 18 files, plus typecheck, lint, and formatting. Branch is merged up to date with main (0.17.1).

claude added 2 commits August 4, 2026 22:04
Re-emit the Emacs-style control keypresses as arrow keys so they move
the cursor in every clack prompt kind, including autocomplete, which
ignores clack's own key alias table.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FyVFq6gYChW9wtoDq8CHsD
Replace the keypress alias listener with an input stream that rewrites
keys before readline decodes them, which the alias approach could not do:
ctrl-j arrives as a line feed that readline submits, wiping the typed
autocomplete filter.

The translated stream also makes the right arrow submit and the left
arrow return to the previous prompt, both only while nothing is typed, so
the caret still works while editing a filter or value. Going back is opt
in per prompt, so a stray left arrow cannot abandon a command, and the
command menu now goes up one level rather than back to the root.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FyVFq6gYChW9wtoDq8CHsD
@razor-x razor-x changed the title Add Emacs-style arrow key aliases (ctrl-p/n) to prompts Navigate prompts with ctrl-p/n/j/k, and go back and forth with the arrow keys Aug 4, 2026
@razor-x razor-x changed the title Navigate prompts with ctrl-p/n/j/k, and go back and forth with the arrow keys Navigate prompts with ctrl-p and ctrl-n Aug 4, 2026
@razor-x
razor-x marked this pull request as ready for review August 4, 2026 23:36
@razor-x razor-x changed the title Navigate prompts with ctrl-p and ctrl-n feat: Navigate prompts with ctrl-p and ctrl-n Aug 4, 2026
@razor-x
razor-x merged commit df68ed9 into main Aug 4, 2026
12 checks passed
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.

2 participants