Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to bindkey Shift+Enter in zsh?

Tags:

zsh

zshrc

I'm starting to use zsh on macOS Sierra. I would like to have the following key mappings:

  • Enter => accept-line
  • Shift-Enter => accept-and-hold

However, I can't seem to differentiate between the two. I'm only able to get Enter, and Esc-Enter, but not Shift-Enter:

bindkey "^M"   accept-line     # Enter
bindkey "^[^M" accept-and-hold # Esc-Enter
bindkey "????" accept-and-hold # Shift-Enter

Is it possible to detect and handle Shift-Enter?

like image 658
Joseph Pecoraro Avatar asked Apr 08 '17 07:04

Joseph Pecoraro


1 Answers

zsh (as well as other shells) do not act on key bindings but rather on key sequences received from the terminal. Converting key presses and combinations into key sequences is the responsibility of the terminal. You can retrieve the key sequence for a key combination by pressing Ctr+v followed by the key combination, e.g. Shift+Enter.

By default Enter and Shift+Enter (as well as Ctrl+v and Ctrl+Shift+m) all generate the identical key sequence ^M (at least in most common terminal emulators).

Fortunately, some terminal emulators allow to configure the key sequences sent. For example iTerm2 allows you to set customized key bindings that send escape sequences (in Profile > Keys), you should be able to define a sequence for Shift+Enter there, e.g. [[SE and can then make the appropriate settings in zsh: bindkey '^[[[SE' 'accept-and-hold'. (Unfortunately I do not have access to a Mac at the moment, so I could not test this).

like image 199
Adaephon Avatar answered Nov 05 '22 13:11

Adaephon