Skip to content

Conversation

@ngxson
Copy link
Collaborator

@ngxson ngxson commented Dec 7, 2025

Fix #17828

Fix #17829

To test it, run llama-cli, write some text (without sending it) and use left/right arrows to edit them

Edit: Home/end keys and history support is allso added thanks to @ServeurpersoCom

Screen.Recording.2025-12-08.at.11.29.15.mov

Disclosure AI usage: next_utf8_char_pos is written by AI because I have no experience working with windows API. The rest is mostly written by me (minus some small auto-completion)

@ngxson ngxson marked this pull request as ready for review December 7, 2025 10:11
@ngxson ngxson requested a review from ggerganov as a code owner December 7, 2025 10:11
@ngxson
Copy link
Collaborator Author

ngxson commented Dec 7, 2025

Thanks @ServeurpersoCom for testing & adding some improvements on windows!

CC @ggerganov , this is a small feature but quite important for the UX - hope to get it merged soon!

@ngxson ngxson force-pushed the xsn/cli_arrow_left_right branch from cd05610 to 77ca259 Compare December 7, 2025 12:29
@ngxson ngxson changed the title console: allow using arrow left/right to edit the line (with UTF-8 support) console: allow using arrow left/right, home/end keys and history mode Dec 7, 2025
@ngxson
Copy link
Collaborator Author

ngxson commented Dec 7, 2025

FYI, I merged some more changes from @ServeurpersoCom to include history too (using up/down keys)

@ngxson
Copy link
Collaborator Author

ngxson commented Dec 8, 2025

pinging @ggerganov in case you missed the PR. I added a video to demo how it works with the current llama-cli

in long term, I think we can remove linenoise and use the common/console.cpp system, as we probably won't use all functionalities from linenoise anyway

Copy link
Member

@ggerganov ggerganov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on Mac, works as expected.

  • Would be nice to add support for "Delete" if possible
  • During chat, when I type a new message and hit "Up + Down" it forgets what I wrote - could be improved.

@ServeurpersoCom
Copy link
Collaborator

ServeurpersoCom commented Dec 8, 2025

I'll add Delete/VK_DELETE support first, and test a temporary buffer for a modern bash-like history!

@ngxson
Copy link
Collaborator Author

ngxson commented Dec 8, 2025

@ServeurpersoCom do you want to add both Delete and Ctrl+Arrow support into this PR? Lmk which commit should I cherry-pick, thanks!

@ServeurpersoCom
Copy link
Collaborator

Bash behavior :
During navigation, you can edit as many old lines as you want, all edits persist.
When you hit Enter on any line (new or edited old one), it gets appended to permanent history -> the cool part: if you edited an old command, the original stays untouched in its position, and your edited version is added at the end.

ngxson and others added 6 commits December 8, 2025 15:45
- Add KEY_CTRL_ARROW_LEFT and KEY_CTRL_ARROW_RIGHT codes
- Windows: detect CTRL modifier via dwControlKeyState
- Linux: parse ANSI sequences with modifier (1;5D/C)
- Implement move_word_left/right with space-skipping logic
- Refactor escape sequence parsing to accumulate params
@ServeurpersoCom
Copy link
Collaborator

@ServeurpersoCom do you want to add both Delete and Ctrl+Arrow support into this PR? Lmk which commit should I cherry-pick, thanks!

I put our work back in this branch (rebased)
master...ServeurpersoCom:llama.cpp:console/terminal-improvement

- Windows: VK_DELETE detection
- Linux: ESC[3~ sequence parsing
- Forward character deletion with UTF-8 support
- Edit any history line during UP/DOWN navigation, edits persist
- Pressing Enter appends edited version as new history entry
- Original line stay untouched in their positions
@ServeurpersoCom
Copy link
Collaborator

ServeurpersoCom commented Dec 8, 2025

Done
https://github.com/user-attachments/assets/005e271e-e973-4e80-a706-e65ffd1612e7


console: implement bash-style history editing

- Edit any history line during UP/DOWN navigation, edits persist
- Pressing Enter appends edited version as new history entry
- Original line stay untouched in their positions

console: add Delete key support

- Windows: VK_DELETE detection
- Linux: ESC[3~ sequence parsing
- Forward character deletion with UTF-8 support

@ngxson
Copy link
Collaborator Author

ngxson commented Dec 8, 2025

just tested the history function on windows and mac, works fine!

@ngxson
Copy link
Collaborator Author

ngxson commented Dec 9, 2025

@ggerganov could you test again to see if the 2 problems in your last comment are all resolved? Thanks!

@ggerganov
Copy link
Member

Yes, they are fixed.

Btw, the color of the first message is white and the next messages are green, is this expected?

image

This is with:

llama-cli -m ../models/gpt-oss-20b/ggml-model-mxfp4.gguf -c 0

@ggerganov
Copy link
Member

ggerganov commented Dec 9, 2025

Looks like this behaviour changed in #17827, so not related to this PR.

@ngxson
Copy link
Collaborator Author

ngxson commented Dec 9, 2025

Btw, the color of the first message is white and the next messages are green, is this expected?

Yes I discovered that in #17824 too, the problem is that the console.cpp flush the stdout immediately (including setting colors), while log.cpp queue the log message.

My quick fix on the other PR is to directly use printf in the CLI, but not quite sure if there is another solution

@ngxson ngxson merged commit 4e842d5 into ggml-org:master Dec 9, 2025
78 checks passed
@ngxson ngxson mentioned this pull request Dec 9, 2025
6 tasks
@bandoti
Copy link
Collaborator

bandoti commented Dec 9, 2025

There was a race condition on the colors I fixed when adding the reasoning to llama-cli. The problem is the tight coupling to the log routine. Please see #16603 changes to console.h/cpp

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.

Feature request: add support for up/down history in console::readline Feature request: add support for left/right arrow for console::readline

4 participants