67

I was trying to switch from a tab to another tab (which may not be adjacent to the previous tab) in VIM. Is there any shortcut for that, like we have Ctrl-p/Ctrl-n for switching to adjacent tabs?

Also, I was trying to write a key mapping which will give a variable as input to a function and do the operation. For instance, let's say I press Ctrl-5 and a function (written by the user) will be called and given as input 5, and the cursor will go to tab 5 (if there is any 5th tab opened).

Can you please suggest how this can be done?

Hulk1991
  • 3,079
  • 13
  • 31
  • 46
Sumit
  • 2,242
  • 4
  • 25
  • 43
  • This should be split into two seperate questions, so you can accept two seperate answers for the two distinct questions. – James Polley Jan 05 '10 at 10:05
  • @James, to know whether this is for two questions, one should know whether a built-in function for switching to N-th tab exists. ;-) – P Shved Jan 05 '10 at 10:16
  • Thanks for all the answers. But how one can write a function which accepts a variable input. writing nmap tt :tabnew seems easy but how binding :tabnew x (where x is a variable) to a key can be done? – Sumit Jan 05 '10 at 10:22
  • :tabnew 4 creates a tab named "4" – Antony Hatchkins Jan 05 '10 at 10:46
  • @Antony Hatchkins, :tabnew doesn't accept any params - it simply creates tabs. To allow naming you should use tabline and guitablabel. – Victor Farazdagi Aug 21 '10 at 08:21
  • 1
    @Victor Farazdagi, :tabnew accepts the following params: "[++opt] [+cmd] {file}". Yes, "4" will be the name of the new empty unsaved file, rather than the name of the tab itself. – Antony Hatchkins Aug 23 '10 at 18:26

3 Answers3

98

use 5gt to switch to tab 5

:tabn[ext] {count}

{count}gt

Go to tab page {count}. The first tab page has number one.

you can also bind it to a key:

:map <C-5> 5gt
:imap <C-5> <C-O>5gt

(Mapping Ctrl-<number> could be different/impossible for some terminals. Consider Alt-<number> then)

Community
  • 1
  • 1
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
21

Tackling only your first question, and quoting from help tabs in vim:

{count}gt       Go to tab page {count}.  The first tab page has number one.
{count}gT       Go {count} tab pages back.  Wraps around from the first one
                to the last one.

ie, typing 3gt goes to the third tab, 3gT goes 3 tabs back from the current tab.

James Polley
  • 7,977
  • 2
  • 29
  • 33
5

Just for sharing the key mapping to jump into particular tab directly. Please put them into _vimrc and make it work.

" Jump to particular tab directly
"NORMAL mode bindings for gvim
noremap <unique> <M-1> 1gt
noremap <unique> <M-2> 2gt
noremap <unique> <M-3> 3gt
noremap <unique> <M-4> 4gt
noremap <unique> <M-5> 5gt
noremap <unique> <M-6> 6gt
noremap <unique> <M-7> 7gt
noremap <unique> <M-8> 8gt
noremap <unique> <M-9> 9gt
noremap <unique> <M-0> 10gt

"INSERT mode bindings for gvim
inoremap <unique> <M-1> <C-O>1gt
inoremap <unique> <M-2> <C-O>2gt
inoremap <unique> <M-3> <C-O>3gt
inoremap <unique> <M-4> <C-O>4gt
inoremap <unique> <M-5> <C-O>5gt
inoremap <unique> <M-6> <C-O>6gt
inoremap <unique> <M-7> <C-O>7gt
inoremap <unique> <M-8> <C-O>8gt
inoremap <unique> <M-9> <C-O>9gt
inoremap <unique> <M-0> <C-O>10gt
Alfred Chan
  • 76
  • 1
  • 1