Skip to content

fix(cli): support Shift+Enter for newline in modern terminals#852

Closed
ZealousEar wants to merge 1 commit intoNousResearch:mainfrom
ZealousEar:fix/shift-enter-keybinding
Closed

fix(cli): support Shift+Enter for newline in modern terminals#852
ZealousEar wants to merge 1 commit intoNousResearch:mainfrom
ZealousEar:fix/shift-enter-keybinding

Conversation

@ZealousEar
Copy link

Summary

  • Modern terminals using the Kitty keyboard protocol (Ghostty, Kitty, WezTerm) or xterm modifyOtherKeys send distinct escape sequences for Shift+Enter (\x1b[13;2u and \x1b[27;2;13~), but prompt_toolkit maps both to plain ControlM (Enter) — so pressing Shift+Enter submits the message instead of inserting a newline.
  • This remaps both sequences to Keys.ControlJ, which triggers the existing Ctrl+Enter handler that inserts a newline. Shift+Enter now works identically to Alt+Enter and Ctrl+Enter for multi-line input.
  • Moves the from prompt_toolkit.keys import Keys import from a local scope to module level, fixing an UnboundLocalError that could occur when Keys was referenced before the local import line.
  • Updates /help text to list all three multi-line shortcuts.

Details

The Kitty keyboard protocol encodes modifier+key as CSI sequences:

Shortcut Escape Sequence prompt_toolkit default After this PR
Shift+Enter \x1b[13;2u Keys.ControlM (submit) Keys.ControlJ (newline)
Shift+Enter \x1b[27;2;13~ Keys.ControlM (submit) Keys.ControlJ (newline)

No behavioral change for terminals that don't use these protocols — plain Enter still submits as before.

Test plan

  • Verified in Ghostty: Shift+Enter inserts newline, Enter submits
  • Alt+Enter and Ctrl+Enter continue to work as before
  • Hermes launches without ValueError or UnboundLocalError
  • Verify in Kitty terminal
  • Verify in WezTerm

🤖 Generated with Claude Code

Modern terminals (Ghostty, Kitty, WezTerm) using the Kitty keyboard
protocol or xterm's modifyOtherKeys send distinct escape sequences for
Shift+Enter (\x1b[13;2u and \x1b[27;2;13~), but prompt_toolkit maps
them to plain ControlM (Enter), which submits the message instead of
inserting a newline.

This patch remaps both sequences to ControlJ so they trigger the
existing Ctrl+Enter handler that inserts a newline. Also moves the
prompt_toolkit Keys import to module level (was a local import that
caused an UnboundLocalError in certain code paths) and updates the
/help text to list all three multi-line shortcuts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@teknium1
Copy link
Contributor

Closing — we've extensively tested ANSI_SEQUENCES remapping for Shift+Enter and unfortunately it doesn't work reliably in practice. Thanks for the effort though!

@teknium1 teknium1 closed this Mar 11, 2026
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