Skip to content

Commit 3f0786d

Browse files
committed
repl: add block-based history navigation
When recalling a multiline entry from history, the Up and Down arrow keys now navigate between history entries as whole blocks rather than moving line-by-line within a recalled entry. Previously, recalling a multiline function from history and pressing Up would move the cursor within that entry's lines before advancing to the previous history entry. With this change, Up/Down always jump directly to the previous/next history entry, matching the behavior of Ctrl-P/Ctrl-N. Fixes: #48146 Signed-off-by: hemanth <hemanth.hm@gmail.com>
1 parent 9041ad0 commit 3f0786d

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

doc/api/repl.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ function myWriter(output) {
395395
}
396396
```
397397

398+
### Block-based history navigation
399+
400+
<!-- YAML
401+
added: REPLACEME
402+
-->
403+
404+
When recalling a multiline entry from history, the Up and Down arrow
405+
keys navigate between history entries as whole blocks rather than
406+
moving the cursor line-by-line within the recalled entry. Up/Down
407+
always jump directly to the previous/next history entry, making it
408+
easy to recall multiline functions and code blocks with a single
409+
keypress — matching the behavior of Ctrl+P and Ctrl+N.
410+
398411
## Class: `REPLServer`
399412

400413
<!-- YAML

lib/repl.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ const {
149149
kMultilinePrompt,
150150
kAddNewLineOnTTY,
151151
kLastCommandErrored,
152+
kHistoryPrev,
153+
kHistoryNext,
152154
} = require('internal/readline/interface');
153155

154156
const parentModule = module;
@@ -702,7 +704,19 @@ class REPLServer extends Interface {
702704
}
703705
clearPreview(key);
704706
if (!reverseSearch(d, key)) {
705-
ttyWrite(d, key);
707+
// Up/Down navigate history in blocks rather than moving
708+
// line-by-line within a recalled multiline entry, making
709+
// arrow-key navigation behave like Ctrl-P / Ctrl-N.
710+
// See: https://github.com/nodejs/node/issues/48146
711+
if (key.name === 'up' || key.name === 'down') {
712+
if (key.name === 'up') {
713+
self[kHistoryPrev]();
714+
} else {
715+
self[kHistoryNext]();
716+
}
717+
} else {
718+
ttyWrite(d, key);
719+
}
706720
const showCompletionPreview = key.name !== 'escape';
707721
showPreview(showCompletionPreview);
708722
}

0 commit comments

Comments
 (0)