Can terminals detect <Shift-Enter> or <Control-Enter>?

21,353

Solution 1

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.

Solution 2

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.

Share:
21,353

Related videos on Youtube

Nicolas Wu
Author by

Nicolas Wu

Updated on November 24, 2020

Comments

  • Nicolas Wu
    Nicolas Wu over 3 years

    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↵!

    • Mykola Golubyev
      Mykola Golubyev about 15 years
      Do you use the vim or vi in the terminal?
  • Nicolas Wu
    Nicolas Wu about 15 years
    It seems that in the terminal different codes are in fact generated: gvim: <C-V><Enter> produces ^M vim: <C-V><Enter> produces ^M gvim: <C-V><C-Enter> produces <C-CR> vim: <C-V><C-Enter> produces ^@ However, I can't seem to map ^@ appropriately, when using: map ^@ command
  • Nicolas Wu
    Nicolas Wu about 15 years
    See vim :help :NL-used-for-Nul for why I got the result I did.
  • Ferdinand Prantl
    Ferdinand Prantl over 10 years
    My gnome-terminal on Ubuntu sens <NL> and imap <C-J> catches it. Thanks!
  • Andrija Čehko
    Andrija Čehko about 7 years
    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?
  • joelostblom
    joelostblom almost 7 years
    @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.