Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in vim, enter is being mapped to a ctrl key custom key map

Tags:

vim

mapping

I am encountering some strange behavior with a keymap in vim that uses the ctrl key. I would guess that this has a simple cause and solution, but I just can't see it.

During the editing of restructuredtext, I find myself typing things like

:math:`x` 

often (this :math: role will cause whatever is inside the ticks to be typeset as math in e.g. the latex output).

I want to map a key like m to enter :math:`` into the text and position the cursor inside the ticks.

I have done this

map m i:math:``ha

and that seems to work fine.

However, I would like to be able to use this map in insert mode. For that, I thought that using ctrl+m would be best. I've done

imap <c-m> :math:``ha

Although that correctly inputs :math:`` and positions the cursor inside the ticks when I do ctrl+m, the trouble is that after this point, every time I press enter in insert mode, it runs the same command as if I typed ctrl+m. In other words, enter in insert mode now seems to be mapped to

:math:``ha

as well.

It seems like it is definitely something to do with using the ctrl key. If I bind e.g. the F5 key as follows

imap <F5> :math:``ha

everything is fine.

I can use the e.g. F5 key and save myself any further bother, but I would like to know what is going on for future reference.

Is there something basic about the use of the ctrl key in a key map that I am missing?

thank you,

like image 940
mjandrews Avatar asked Dec 26 '22 06:12

mjandrews


2 Answers

You have to use a different control combination for your mapping, e.g. <C-g>.

Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today without these side effects, even in GVIM. Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) This also applies to <Tab> / <C-I>, <CR> / <C-M> / <Esc> / <C-[> etc. (Only exception is <BS> / <C-H>.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

like image 172
Ingo Karkat Avatar answered Mar 06 '23 08:03

Ingo Karkat


if you do a

:h keycodes

you will see:

notation    meaning         equivalent  decimal value(s)    ~
-----------------------------------------------------------------------
....

<CR>        carriage return     CTRL-M   13 *carriage-return*
<Return>    same as <CR>                *<Return>*
<Enter>     same as <CR>                *<Enter>*

so it tells, <c-m> is same as <Enter> (same keycode 13)

you could test in your shell too, for example, type ls and then <c-m> instead of <Enter>

like image 33
Kent Avatar answered Mar 06 '23 08:03

Kent