Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the Ctrl+0 through Ctrl+9 key bindings be mapped in Vim?

Tags:

Why can’t I map—using any of the *map commands (nmap, imap, etc.)—the Ctrl+0 through Ctrl+9 key strokes? In fact, it appears that some of them, like Ctrl+3, are bound to ^[ at the X Window level. How can I make Vim override those default bindings?

That is, if I do imap <C-3> fancystuffhere, typing Ctrl+3 in Insert mode puts me into Normal mode, instead of inserting 'fancystuffhere'.

I’m using X11 on Linux.

like image 210
cespinoza Avatar asked Jan 18 '11 20:01

cespinoza


People also ask

How to map control key in Vim?

On MS-Windows, if the mswin. vim file is used, then CTRL-V is mapped to paste text from the clipboard. In this case, you can use CTRL-Q or CTRL+SHIFT+V instead of CTRL-V to escape control characters. In the above command to enter a single ^V, you have to press Ctrl and v.

What is silent in Vim?

<silent> tells vim to show no message when this key sequence is used. <leader> means the key sequence starts with the character assigned to variable mapleader -- a backslash, if no let mapleader = statement has executed yet at the point nmap executes.


2 Answers

Both Vim and gVim use byte queue instead of keypress event queue, so that values from 0x40 (@) to 0x5F (_), including the 0x41–0x5A (AZ) range) have corresponding control characters (you can get their codes by subtracting 0x40 from their value). Because of this, no characters above and beyond this range can be used together with C- (Ctrl).

It is also the reason why C-S- (Ctrl+Shift) for alphanumeric keys does not work even in gVim—functional keys generate more then one byte, so <C-S-F1> may work. Replacing <C-3> with <Esc> is done by terminal; you can try mapping it in almost any GUI application and see that <Esc> does not get mapped.

like image 75
ZyX Avatar answered Sep 27 '22 22:09

ZyX


The way I accomplish this on windows is to use Autohotkey and remap Ctrl+1, Ctrl+2, etc. to Ctrl+F1, Ctrl+F2, etc.

I know you’re on Linux, so it doesn’t apply for you, but thought I’d mention it for anyone else interested.

like image 28
Steve Vermeulen Avatar answered Sep 27 '22 22:09

Steve Vermeulen