Can terminals detect <Shift-Enter> or <Control-Enter>?
Asked Answered
W

2

50

Is it possible for the terminal to detect ⇧ Shift+Enter↵ or Ctrl+Enter↵ keypresses?

I am trying to configure vim to do key mappings that use these sequences, and while they work fine in gvim, they don't seem to work in any terminal console.

The curious thing is that although Ctrl+Enter↵ is not detected in vim, mapping Enter↵ to Esc maps properly, but then pressing Ctrl+Enter↵ behaves like Enter↵!

Wheeler answered 28/2, 2009 at 14:57 Comment(1)
Do you use the vim or vi in the terminal?Abash
W
50

Some terminals send <NL> when <C-Enter> is pressed. This is equivalent to sending <C-J>.

To find out what your terminal does with <Shift-Enter>, <Ctrl-Enter> and <Enter>, go to your terminal, type <Ctrl-V> (similar to sykora's suggestion for vim), and type in the sequence you're interested in.

Using gnome-terminal, I get the following:

  <Enter> : ^M
<S-Enter> : ^M
<C-Enter> : <NL>

Looking at man ascii indicates that ^M gives the <CR> sequence.

The answer is that it depends on the terminal, and there's an easy way to check.

Wheeler answered 28/2, 2009 at 18:9 Comment(3)
My gnome-terminal on Ubuntu sens <NL> and imap <C-J> catches it. Thanks!Lallation
I am also using gnome-terminal but I get I get different results: <Enter> : ^M, <S-Enter> : ^M, <C-Enter> : ^M. Is there any way to fix this?Jackjackadandy
@AndrijaČehko I am getting the same results as you in terminator, which is based on gnome-terminal. You can still map ^M in vim (insert that character by pressing Ctrl-V + Enter in insert mode) to whatever command you want and then access it by pressing enter (either with or without holding shift or control). I don't think there is a way to get separate functionality from Enter, Shift-Enter, and Ctrl-Enter in this case.Isocrates
S
22

Gvim runs its own manager for keystroke handling and so can pick up all the various key combinations. Vim is reliant on the specific terminal for passing on the particular keypress, so keyhandling is only as good or varied as the terminal is.

One way you can find out whether you can do what you want to do is to use the key to find out what is inserted. eg Type:

:<C-V><C-Enter>

ie actually type in the combination you want to press after having typed the combination Control-V. After that do the same thing for enter, ie

:<C-V><Enter>

If they yield the same code, then the terminal interprets both key combinations as the same keycode, and you can't bind them without messing with the terminal.

In my terminal (urxvt), Control-Enter, Shift-Enter and Enter (by itself) all produce the ^M character, meaning I can't map one without mapping the other. The same goes for Control-Tab and Control-I, and Control-Space and Control-@

EDIT: Use C-Q instead of C-V for Windows.

Spitsbergen answered 28/2, 2009 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.