views:

860

answers:

2

Is it possible for the terminal to detect < Shift-Enter > or < Control-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 < C-Enter > is not detected in vim, mapping < Enter > to < Esc > maps properly, but then pressing < C-Enter > behaves like < Enter >!

+4  A: 

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.

sykora
It seems that in the terminal different codes are in fact generated:gvim: <C-V><Enter> produces ^Mvim: <C-V><Enter> produces ^Mgvim: <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
What terminal are you using?
sykora
Using gnome-terminal.
Nicolas Wu
See vim :help :NL-used-for-Nul for why I got the result I did.
Nicolas Wu
+4  A: 

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.

Nicolas Wu