Reputation: 6869
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.
Upvotes: 21
Views: 6077
Reputation: 22596
One way to do this (depending on your terminal) is to remap (in your terminal) the return
key to C-J
.
C-J seems to act like return in pretty much every terminal application.
Upvotes: 3
Reputation: 3387
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>
Upvotes: 2
Reputation: 2392
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.
Upvotes: 2
Reputation: 79175
: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.
Upvotes: 10