6

How can I use a variable when mapping keys in vim? The specific problem that I am trying to solve is the following. I need these key mappings:

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

... and so on.

Can I specify one mapping; something like

nnoremap <C-x> xgt

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

Thank you.

Edit 1: Towards the solution (not yet complete) thanks to Peter Rincker

I can use the function

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

If I :call goToTab(3), it goes to tab 3.

How do I map Command-x (D-x) to goToTab(x) where x is between 1..9. How do I read the number from a Command-x press?

1 Answer 1

6

I got bad news. You can not map <c-1>, etc. You can only bind <c-6> which I wouldn't do as it is very handy.

It also seems like you are doing a heavily tab centric workflow. I know it might sound weird but maybe use less tab panes and more buffers. Here are some nice posts about it:

... Ok, but I really want to do this variable mapping thing. You have options:

  • Use a for loop and use :execute to create mappings
  • The more Vim Way is to use a count so 7gt. The 7 is the count.

Example of using :for and :execute:

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

Note: this uses <d-...> syntax for Command which is only available on MacVim and no terminal support (See :h <D-). You can use <a-...> for Alt. However I must warn you using Alt on the 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(
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the information about buffers. I am new to vim and was not even aware of them. While I will read and look up on buffers, just for the purpose of learning Vim I want to try and implement the above (say with the command key instead of the control key). Please see my edited message above. I am one step closer to the solution due to your other pointers. Can you or someone help me with the next step (or the solution). Thanks!
Maybe try using alt-mappings instead of ctrl-mappings, then. I'd go with the for loop.
@Ben How do I read the number (whether Alt or Command) as an input to the loop or to a function call? Would love an example. I think for-loop will not work since it will start counting from the current tab. That is not what say 2gt does. Thank you.
Looks like an example got added. It will not start counting from the current tab, and that is not what 2gt will do anyway. 2gt will always go to the 2nd tab page, starting from the leftmost tab, regardless of what tab you're on.
If you want this to count from the current tab you can use the expression register. Change your :execute line look like so: execute "nnoremap \<d-" . i . "> " . i . "@='gt'\<cr>". For more information see :h @, :h @=, and this Vimcast episode: Creating mappings that accept a count
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.