Strovent SystemsSign in
Reference

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: ESC followed by 0D, because Alt adds an ESC prefix.
  • 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 m to set level 2. Keys held with a modifier then arrive as CSI 27 ; modifier ; code ~, where Shift is modifier 2 and the code for Enter is 13. Shift+Enter becomes ESC [ 27 ; 2 ; 13 ~.
  • kitty keyboard protocol ("CSI u"). The program asks for enhanced reporting with CSI > flags u. Keys are reported as CSI number ; modifiers u. Enter is number 13 and Shift is modifier value 2, so Shift+Enter is ESC [ 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:

  • ^M means a plain carriage return, the same as Enter.
  • ^[^M means ESC plus carriage return (Alt+Enter).
  • Something like ^[[27;2;13~ or ^[[13;2u means 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 ESC plus 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-keys on.

Sources

Spotted a mistake? Email [email protected].