feat: Navigate prompts with ctrl-p and ctrl-n - #605
Merged
Conversation
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
…orth" This reverts commit f4e10c3.
razor-x
marked this pull request as ready for review
August 4, 2026 23:36
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 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.tsand its tests.Implementation
A single
keypresslistener onprocess.stdinre-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/nwould 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'sh/j/k/lletters 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.tscovers the key mapping — ctrl-p/n map to up/down, and plainp/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).