Using variable in vim key mappings

How can I use a variable when matching keys in vim? The specific problem I'm trying to solve is as follows. I need these key mappings:

nnoremap <C-1> 1gt
nnoremap <C-2> 2gt
nnoremap <C-3> 3gt

... and so on.

      

Whether it is possible to specify one mapping; something like

nnoremap <C-x> xgt

      

where x is the value of the key pressed (which can be from 1..9)

Thank.

Edit 1: Towards a solution (not yet complete) thanks to Peter Rinker

I can use the function

function gotoTab(num)
   execute "normal" a:num."gt"
endfunction

      

If I am :call goToTab(3)

, it goes to tab 3.

How to match Command-x (Dx) with goToTab (x) where x is between 1..9. How do I read the number from Command-x?

+3


source to share


1 answer


I have some bad news. You cannot match <c-1>

, etc. You can only bind <c-6>

, which I would not do as it is very convenient.

It also seems like you are doing a large tabbed workflow. I know this may sound strange, but maybe use fewer tabs and more buffers. Here are some good posts about this:

... Ok, but I really want to make this variable. You have options:

  • Use a for loop and use :execute

    to create mappings
  • The more Vim Way uses the counter 7gt

    . 7

    is a counter.


Usage example :for

and :execute

:

for i in range(1, 9)
  execute "nnoremap \<d-" . i . "> " . i . "gt"
endfor

      

Note: This uses the <d-...>

command syntax available only on MacVim and no terminal support (see :h <D-

). You can use <a-...>

for Alt. However, I must warn you that using Alt on a terminal can be tricky.

For more help see:

:h keycodes
:h map-which-keys
:h :for
:h :exe
:h count
:h v:count
:h range(

      

+5


source







All Articles