tags:

views:

63

answers:

3

I want to map Ctrl-M to Ctrl-N in insert mode. If I simply do imap <C-M> <C-N> then Ctrl-M does start to behave just like Ctrl-N, but then hitting Enter does the same as well. I want pressing Return to keep inserting new lines, and at the same time make Ctrl-M insert the next keyword completion match just like Ctrl-N does. Is that possible?

EDIT: I managed to modify Vim's source code to unconditionally treat Ctrl-M as Ctrl-N without affecting Return. While doing so I also realized that indeed there is no way to do that without source code change, as the distinction between what has actually been pressed - Enter or Ctrl-M, appears to vanish much too early in key-press processing. It happens in platform-dependent UI modules, and the portable code part in key-press handling already has no idea if Ctrl-M or Return was actually pressed that resulted in key code 13.

My modifications were in GUI modules for FreeBSD (GTK) and Windows, as those are the platforms I use gvim on most often.

P.S. If anyone ever wants to achieve the same, please feel free to drop me a note.

P.P.S. To all who have provided answers to this question: thank you very much! Your comments helped me a lot.

+2  A: 

Hello. :help key-notation clearly states that CTRL-M is equivalent to Enter. This is because carriage return is ASCII character no. 13 and M is the 13th letter of the alphabet.

Benoit
Thanks for your answer. Yes, I see the connection between `\r` and Ctrl-M for M being the 13th letter. But isn't there a way to somehow detach that association in Vim? Other than modifying the source code, that is :)
usta
+1  A: 

The following will treat CTRL-M (or Enter as Benoit pointed out) as CTRL-N when the popup menu is visible and revert to its default operation when the popup menu is not visible.

:inoremap <C-M> <C-R>=pumvisible() ? "\<lt>C-N>" : "\<lt>C-M>"<CR>

This requires you to invoke the popup menu using CTRL-N or CTRL-P prior to using CTRL-M as CTRL-N.

tinifni
Thank you for your answer. Probably I should have clarified why and how I need this. If Ctrl-M takes it's new function only after popup is visible, it's too late. Basically I need to make Ctrl-M and Enter do different things.
usta
Here's why I need that: all too often when I want to press Ctrl-N my finger goes wrong and presses Ctrl-M, so instead of inserting the next keyword completion match it inserts a new line, which is very annoying experience for me. I have no use for Ctrl-M inserting a new line, so I just want to permanently change its function to behave just like Ctrl-N; I'll use Enter for inserting new line as always, never need Ctrl-M do the same. With Ctrl-M inserting next match, I won't even notice I pressed the wrong key, and my (g)Vim experience will become much happier. Thanks for reading thus far!
usta
I am pretty certain you cannot break the link between <C-M> and <Enter>. I hope you find your answer.
tinifni
+1  A: 

If you are using Windows, you can probably use Autohotkey to re-map Ctrl-M while in insert mode if the active window is vim.

The ahk script would probably look something like this:

 #IfWinActive, Write: ahk_class GVIM
 ^M::^N

Alternately, you might consider working on your touch typing skills (I doubt you want to hear that solution, but I still think it's a valid suggestion) to avoid getting Ctrl-M and Ctrl-N confused.

If you are using some flavor of Linux, I am not sure what tools could be equivalent to autohotkey.

Another alternative is to map a completely different key to let you do autocompletion, instead of using ctrl-n, such as Ctrl-K: :inoremap <c-k> <c-n>

Kimball Robinson
@k.robinson: Thank you, these are all valid suggestions indeed (touch typing skills improvement too, even if less preferred!). Sometimes I do use Windows, but primarily I use FreeBSD so Autohotkey is not really a solution for me. +1 for the completely different key suggestion, very nice one actually, I'll keep that in mind. But first I'll try my luck at hacking Vim's source code, and if that doesn't work well I'll seriously consider your suggested approach. Thanks again for your help!
usta