0

I'd like to map +Ngt and -Ngt to :tabnext N and :tabprev N. I can create key bindings such as:

nnoremap <buffer> <silent> +1gt :tabnext 1
nnoremap <buffer> <silent> +2gt :tabnext 2
nnoremap <buffer> <silent> +3gt :tabnext 3
nnoremap <buffer> <silent> +4gt :tabnext 4
nnoremap <buffer> <silent> +5gt :tabnext 5
nnoremap <buffer> <silent> +6gt :tabnext 6
...

but can I do it better? (Not that I really want to move "6 tabs ahead" since that would be much slower than just typing Ngt, but IMO it's only natural to ask this question for future reference.)

A great example would be NG command that jumps to line N. I highly doubt that Vim internally adds separate keybinding to each line we might possibly want to jump to.

1

Yes, you can do better:

nnoremap <silent> gt @=":tabnext\n"<CR>

Then 6gt will move 6 tabs ahead etc. Using the expression register = is a trick to make a map repeatable by a number prefix.

On a side note: you can also do better by posting Vim-related questions to its own home. :)

1
  • Right, I completely forgot about vim stackexchange. Thanks.
    – rr-
    May 17 '15 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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