system/readline: Add support for line movements and editing

This commit adds support to nsh> command line editing. This is one
of the most missing feature of the NuttShell. If you typed a long
command line and make a mistake you need to press Backspace and
remove everything until reach that typo. Only the basic editing
feature is enabled by default (left/right keys movement, Home/End
to move to the beginning or ending of the command line).

More advanced features are available when CONFIG_READLINE_EDIT_EMACS
is selected. It enables the Emacs-style control keys (Ctrl+A/B/D/E/F/K/U/W)
that allow more flexible line editing.

Other more advanced feature is enabled when CONFIG_READLINE_CMD_HISTORY
and CONFIG_READLINE_EDIT_EMACS_REVERSE_SEARCH are enabled. It allows the
user to press Ctrl+R to do reverse search in the command line history.

Signed-off-by: Alan C. Assis <acassis@gmail.com>
Assisted-by: Claude Code
This commit is contained in:
Alan C. Assis 2026-07-14 21:39:39 -03:00
parent 0304721c85
commit 51bfd48e78
3 changed files with 925 additions and 62 deletions

View file

@ -223,6 +223,12 @@ int nsh_session(FAR struct console_stdio_s *pstate,
write(OUTFD(pstate), nsh_prompt(), strlen(nsh_prompt()));
#ifdef CONFIG_READLINE_TABCOMPLETION
/* Set the prompt so readline can redraw it for editing features */
readline_prompt(nsh_prompt());
#endif
/* readline() normally returns the number of characters read, but
* will return EOF on end of file or if an error occurs. EOF
* will cause the session to terminate.

View file

@ -54,6 +54,25 @@ config READLINE_MAX_EXTCMDS
endif # READLINE_TABCOMPLETION
config READLINE_EDIT
bool "Command line editing"
default y if !DEFAULT_SMALL
---help---
Build in support for full command-line editing using cursor keys,
Home/End, Delete. Requires a VT100/ANSI-compatible terminal.
if READLINE_EDIT
config READLINE_EDIT_EMACS
bool "Emacs-style control keys"
default n
---help---
Enable emacs-style control key bindings:
Ctrl+A Home, Ctrl+E End, Ctrl+K kill-to-end,
Ctrl+U kill-to-start, Ctrl+Left/Right word movement.
endif # READLINE_EDIT
config READLINE_CMD_HISTORY
bool "Command line history"
default n
@ -96,6 +115,17 @@ config READLINE_CMD_HISTORY_LEN
will be READLINE_CMD_HISTORY_LINELEN x READLINE_CMD_HISTORY_LEN.
Default: 16
config READLINE_EDIT_EMACS_REVERSE_SEARCH
bool "Reverse incremental search (Ctrl+R)"
default y
depends on READLINE_EDIT_EMACS
---help---
Enable bash-style Ctrl+R reverse incremental search through the
command history. Typing narrows the search to the most recent
matching command; Ctrl+R again searches further back; Enter
submits the match; Ctrl+G/Ctrl+C cancels back to the line being
edited before Ctrl+R was pressed.
endif # READLINE_CMD_HISTORY
endif # READLINE_ECHO
endif # SYSTEM_READLINE

File diff suppressed because it is too large Load diff