Mapping keys on numerical keypad - vim

For years now I've been using laptops - without numerical keypads on their keyboards. Having recently acquired a dekstop keyboard ... just today I found out that Vim's mapping to numbers (1 ... 9, 0) don't work on their numerical keypad equivalents. What I mean?
nmap 1 Dj
doesn't work on the numerical keyboard 1?
Is there any solution to this problem (not really a "problem", but annoying)?

This works for me on Windows: nmap <C-k5> :tabnew<CR>
The keypad identifiers are k1, k2, etc. Also kPlus and so on. As far as I can tell, the mappings fail when NumLock is off.

I have had some keyboards and terminals where I was unable to map via the k* identifiers. To map them in that situation, use Ctlv in order to input the correct escape code
So for example to map 1 on the keypad to perform Dj, enter:
:map
Then type Ctlv, which will leave open an escape sequence with ^[. Then press 1 on the keypad. The resultant code will look something like ^[Ow. Complete your mapping as normal:
:map ^[Ow Dj

You can map in vi (e.g. in ~/.vimrc) the NUM keys in the following way:
inoremap <Esc>Oq 1
inoremap <Esc>Or 2
inoremap <Esc>Os 3
inoremap <Esc>Ot 4
inoremap <Esc>Ou 5
inoremap <Esc>Ov 6
inoremap <Esc>Ow 7
inoremap <Esc>Ox 8
inoremap <Esc>Oy 9
inoremap <Esc>Op 0
inoremap <Esc>On .
inoremap <Esc>OQ /
inoremap <Esc>OR *
inoremap <Esc>Ol +
inoremap <Esc>OS -
inoremap <Esc>OM <Enter>
This setting is an example for using the NUM key as they should be. Of course you can change the mappings.

I also use keypad mappings a lot. To help you get started with figuring out mappings, you can see what keys vim "knows about" and what there current mappings are with:
:h keycodes
Hopefully this will list you keypad keys as etc etc. If that is the case, then you can easily (don't have to worry about finding out and using their escape sequences) remap them using map <k0> :wincmd w<CR> for instance.
For years I was using the Ctrl-v followed by a key to get the XXXX mappings previously until I found about the keycodes command, and the really helpful definitions.
Hope that might help!

Related

Vim press <cr> multiple time

How do you simplify this
inoremap <C-S-Tab> <cr><cr><cr><cr>
to this?
inoremap <C-S-Tab> <cr>*4
Being an insert mode mapping,
inoremap <C-S-Tab> <cr><cr><cr><cr>
can't be "simplified" with a count because numbers are printable characters too. You want four <CR>s, your mapping has four <CR>s: it's perfectly fine as-is.
There are a few ways to "complexify" it, though…
inoremap <expr> <C-S-Tab> repeat('<CR>', 4)
uses an "expression mapping" which computes the expression at runtime, see :help <expr>, :help repeat().
inoremap <C-S-Tab> <C-o>4a<CR><Esc>i
leaves insert mode for one normal mode command and then does a<CR> four times before coming back to insert mode.
Note that <C-S-Tab> is not a portable combo as it is indistinguishable from <S-Tab> in most environments.
how about use record function like using 'r' to record return press then use 4#r to execute 4 times?

Properly remap vim numpad keys when numlock is off

I want to remap my numpad so that even if numlock is off, I still have it print numbers.
This is obviously a fairly common task - it has been asked multiple times around the internet, and has its own page dedicated to it (though the page is putty-specific):
https://vim.fandom.com/wiki/PuTTY_numeric_keypad_mappings
Whenever it gets asked online, the answer seems to always be the same - a copy-paste of the lines from that guide:
:inoremap <Esc>Oq 1
:inoremap <Esc>Or 2
:inoremap <Esc>Os 3
:inoremap <Esc>Ot 4
:inoremap <Esc>Ou 5
:inoremap <Esc>Ov 6
:inoremap <Esc>Ow 7
:inoremap <Esc>Ox 8
:inoremap <Esc>Oy 9
:inoremap <Esc>Op 0
:inoremap <Esc>On .
:inoremap <Esc>OQ /
:inoremap <Esc>OR *
:inoremap <Esc>Ol +
:inoremap <Esc>OS -
:inoremap <Esc>OM <Enter>
Unfortunately, that doesn't always work. It certainly works for putty - I can confirm that, but right now, I'm using Fedora 31, with the regular Gnome Terminal, and with those lines in my .vimrc file, if I hit a numpad key with the numlock disabled, the keys either act as arrow keys (2,4,6,8), or Home, End, Pg Up, Pg Down.
One thing I can do is this instead (or in addition to the other mappings):
inoremap ^[OF 1
inoremap ^[OB 2
inoremap ^[[6~ 3
inoremap ^[OD 4
inoremap ^[OE 5
inoremap ^[OC 6
inoremap ^[OH 7
inoremap ^[OA 8
inoremap ^[[5~ 9
inoremap ^[[2~ 0
inoremap ^[[3~ .
inoremap ^[Oo /
inoremap ^[Oj *
inoremap ^[Ok +
inoremap ^[Om -
inoremap ^[OM <Enter>
I got these key combinations but simply hitting Ctrl+V, and my numpad keys to see what signal was being sent when the numlock was off. This works, and my numpad keys work as expected now. Except there's a problem. These signals are exactly the same as the other keys send. This means that now, if I actually hit my left arrow key in insert mode, it prints 4 instead of moving left. Same for the other arrow keys, and the Home/End/PageUp/PageDown keys.
This is obviously inconvenient, because in insert mode, you can't use hjkl to navigate, and now, the arrow keys don't work either.
I understand the reason this happens - with numlock off, my 7 key on the numpad sends the exact same code that the Home button does, so if I override that, it effects both the 7 button (when the numlock is off), and the actual Home button.
Is there any kind of smart fix for this? Or is this inherently impossible to map without effecting the real arrow buttons, and Home/End/PgUp/PgDown buttons?
Failing that, is vim capable of enabling numlock when it starts up?

Remapping <ESC> breaks <S-TAB> mapping in vim

I had the following mapping on my vimrc
nnoremap <TAB> gt
nnoremap <S-TAB> gT
Then I mapped ESC to clear highlights as:
noremap <silent> <ESC> :noh<return>
And then <S-TAB> wont work anymore. If I remove <silent> from <ESC> mapping I see :noh when I press <S-TAB>. I don't know if <S-TAB> and <ESC> has something in common. I'm on Linux, using vim on gnome-terminal.
You're right in assuming that <S-TAB> and <ESC> have something in common. S-TAB is an escape-prefixed keycode.
The ESC keycode is ^[, while S-TAB is ^[[Z. You can see the first part of the S-TAB key code matching the ESC keycode.
See a full table of combinations here.
So you just can't remap the escape key while also remapping one of the key codes including it. Either pick a different key to clear highlights or a different way of switching tabs.

Conflict between tab to insert spaces and superTab

I have conflict right now so that whenever I want to insert a tab (2 spaces since I have set expandtab in .vimrc) I get list options. Currently Im trying to make a mapping
inoremap <C-tab> <C-v-tab>
But this does not work. What am I doing wrong?
If you want to have an alternative mapping for the <Tab> key, that's
inoremap <C-Tab> <Tab>
The noremap part automatically ensures that SuperTab's mappings don't apply, and you get the built-in functionality.
Yours didn't work because of the invalid key notation, Ctrl + V followed by Tab would be (here with :imap to offer a worse alternative):
imap <C-Tab> <C-v><Tab>

Map key to toggle between normal mode and insert mode in Vim

I'd like to use two "controls" as a toggle key to switch between normal mode and insert mode in Vim. So I add the following two lines into my .vimrc
nmap <C-><C-> i
imap <C-><C-> <ESC>
But it doesn't work. What's wrong with the above two lines?
It seems you are trying to map Ctrl+Space to toggle insert mode.
nnoremap <C-space> i
imap <C-space> <Esc>
(Came from this Vim tip (marked obsolete, but there's a link to a more rich document on avoiding which includes the tip).)
Remember that this is not guaranteed to work across all terminals and platforms. Some terminals and platforms may eat a given Ctrl+something shortcut, while others don't, so find one that works in your environment.
nnoremap <silent><C-space> :startinsert
inoremap <silent><C-space> <C-O>:stopinsert
That's definitely not going to work. You could use an F key instead.
nnoremap <C-SPACE> i
inoremap <C-SPACE> <ESC>l
works perfectly with GVIM 7.4

Resources