Shift+Enter or Ctrl+Enter acts like Enter in a terminal: why, and how to fix it
A terminal sends the same carriage return for Enter, Shift+Enter and Ctrl+Enter unless the program turns on an extended key mode. Ctrl+J often works instead.
In a text terminal, Enter sends one byte, a carriage return (0D). Shift+Enter and Ctrl+Enter send that same byte. The program on the host cannot tell them apart, so a "new line" shortcut that relies on Shift+Enter behaves like Enter and submits the line.
This is how terminal keyboards have always worked, not a fault in SSH. SSH carries the bytes your terminal sends without changing them.
What each combination sends
In the standard (legacy) encoding, which every terminal uses unless a program asks for something else:
- Enter, Shift+Enter, Ctrl+Enter, Ctrl+Shift+Enter:
0D(carriage return). - Alt+Enter:
ESCfollowed by0D, because Alt adds anESCprefix. - Ctrl+J:
0A(line feed), a different byte from Enter. - Ctrl+M:
0D, the same as Enter.
So of these, only Alt+Enter and Ctrl+J reach the program as something other than Enter.
How a program can see Shift+Enter
Two extended key modes report modifiers on keys such as Enter. Both are off by default, and the program, not the user, turns them on by sending an escape sequence to the terminal:
- xterm modifyOtherKeys. The program sends
CSI > 4 ; 2 mto set level 2. Keys held with a modifier then arrive asCSI 27 ; modifier ; code ~, where Shift is modifier2and the code for Enter is13. Shift+Enter becomesESC [ 27 ; 2 ; 13 ~. - kitty keyboard protocol ("CSI u"). The program asks for enhanced reporting with
CSI > flags u. Keys are reported asCSI number ; modifiers u. Enter is number13and Shift is modifier value2, so Shift+Enter isESC [ 13 ; 2 u.
Shift+Enter only works when all three agree: the program requests one of these modes, the terminal supports that mode, and everything in between passes it on.
Inside tmux
tmux is a terminal of its own, so the program talks to tmux and tmux talks to your terminal. Modified keys only get through when the tmux option extended-keys is on or always:
set -g extended-keys on
Put it in ~/.tmux.conf, then restart the tmux server or run tmux source-file ~/.tmux.conf. The outer terminal must also support extended keys. If it does not, Shift+Enter still arrives as a plain carriage return.
Check what a key sends
At a bash prompt, press Ctrl+V and then the key. Ctrl+V inserts the next key verbatim:
^Mmeans a plain carriage return, the same as Enter.^[^MmeansESCplus carriage return (Alt+Enter).- Something like
^[[27;2;13~or^[[13;2umeans an extended mode is active.
Press Ctrl+U afterward to clear the line.
What to use instead
- Ctrl+J sends a line feed. Some programs bind it to "insert a new line" while Enter submits; check the program's key list. In bash itself, Ctrl+J runs the line just like Enter.
- Alt+Enter reaches the program as
ESCplus Enter, which some programs bind to a new line. - A trailing backslash (
\then Enter) continues a command onto the next line in bash, zsh and other POSIX shells. - If the program documents Shift+Enter support, it usually needs a terminal that supports modifyOtherKeys or the kitty protocol. Test without tmux first, then add tmux with
extended-keyson.
Related
- Keys and key codes
- Escape sequences
- Escape sequence table
- VT, xterm and terminal types
- Ctrl+Space not working over SSH or in tmux: the NUL byte and how to test it
- Enter vs Return on 3270, 5250 and VT keyboards, and the New Line key
Sources
- kitty keyboard protocol: legacy encodings, progressive enhancement and modifiers
- XTerm control sequences: XTMODKEYS and modifyOtherKeys
- tmux(1) manual: extended-keys
- GNU Bash manual: quoted-insert (C-v)
- GNU Bash manual: accept-line (Newline or Return)
- POSIX Shell Command Language: backslash-newline line continuation