Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: How to map Ctrl-M without affecting Return keypress as well?

Tags:

vim

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.

like image 324
usta Avatar asked Oct 14 '10 17:10

usta


2 Answers

: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.

like image 168
Benoit Avatar answered Oct 13 '22 13:10

Benoit


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>

like image 39
Kimball Robinson Avatar answered Oct 13 '22 12:10

Kimball Robinson