4

Typing source code is easier for me when the numbers row above the letters (the numrow) is permanently shifted, giving me access to the symbols at a single keypress, without pressing Shift. The numbers themselves are available when pressing Shift of course. To achieve this, I use the following in my configuration:

nnoremap 1 !
nnoremap 2 @
nnoremap 3 #
nnoremap 4 $
" etc...

vnoremap 1 !
vnoremap 2 @
vnoremap 3 #
vnoremap 4 $
" etc...

onoremap 1 !
onoremap 2 @
onoremap 3 #
onoremap 4 $
" etc...

" the inverse maps not included here

This works great normally, but I have a problem. The command for 'select everything inside parantheses', namely vi), isn't working as expected, because I still have to press Shift for ), otherwise Vim will receive the command vi0, which isn't what I want.

What I want is to press vi0 but have the 0 mapped to ) without shifting. Unfortunately, onoremap 0 ) didn't have any effect. How can I achieve this?

10
  • 2
    Possible duplicate of Operator pending mode remaps not working
    – filbranden
    Aug 3, 2019 at 16:29
  • Take a look at keymap files, the :loadkeymap command and the 'keymap' option. That's a better fit for what you're trying to do. (I can turn this into an answer if it works for you.)
    – filbranden
    Aug 3, 2019 at 16:40
  • @filbranden As far as I know, keymap only works for insert mode. Does it work in normal and visual modes?
    – CamilB
    Aug 3, 2019 at 17:17
  • 1
    You may just need to onoremap i) and friends
    – D. Ben Knoble
    Aug 3, 2019 at 20:22
  • 1
    @D.BenKnoble My mistake: onoremap i0 i) actually works, but for di0. For vi0, I needed vnoremap i0 i) and it worked. Please post your comment as an answer so I can accept it.
    – CamilB
    Aug 4, 2019 at 9:02

1 Answer 1

2

Vim maps will handle commands, not individual keystrokes, so if you want to remap your keys, you'll need to handle two character commands which use one of those keys explicitly.

In the example you brought up, vi), you'll want to create new Visual and operator-pending mappings for i) (and a) which is similar):

onoremap i0 i)
vnoremap i0 i)
onoremap a0 a)
vnoremap a0 a)

But then you'll see that there are more such two character commands that use the symbols on those keys, and will want to remap those as well. For example, g# and g*:

nnoremap g3 g#
nnoremap g8 g*

And then you'll start finding more and more.

Maybe it's not that bad, but it feels like it's not the best solution for this problem. I think I'd recommend looking into keyboard mappings at the O.S. level, or at least terminal emulator. Some advantages of that would be:

  • That would work in any program, not just Vim.
  • That would work in Vim regardless of mode and whether the mapped character is part of a multi-character command.
  • That would work even for new mappings introduced by plug-ins that include the remapped symbols.
  • You could remap the numeric keys on the top row, but leave the keypad untouched so it would still produce numbers (with, or perhaps even without, NumLock.) With the Vim mappings, you're affecting all of them the same, since you can't tell them apart.

So I think there are better solutions to this problem than Vim mappings... But if you go that way, then consider the multi-character mappings as part of what you need to handle.

3
  • 1
    I already wrote a new keyboard layout in X and was using it successfully, until I noticed the computer was missing keypresses when switching windows (due to the policy of one layout per individual window). This is why I wanted to shift the numrow just in Vim, and maybe just in specific languages (CSS is better with the normal numrow). I'm still not sure what's better though. I'm already finding new commands that need remapping, as you said.
    – CamilB
    Aug 4, 2019 at 14:06
  • 1
    Consider a global keyboard layout in X and keep two layouts around with a key combination to switch between them, then you don't have the problem with switching windows (since the layout is global) and you can switch back to numbers on the top row when convenient. (I'm no expert on X, so not 100% sure this is doable, but I'd imagine it would be.)
    – filbranden
    Aug 4, 2019 at 14:13
  • Yes, a global layout is actually the default policy. Will give it a try, it does seem like a simpler solution indeed.
    – CamilB
    Aug 5, 2019 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.