In vim, how can I quickly switch between tabs?

146

65

In gnome-terminal, I can just press Alt + (1, 2, 3, etc.) to switch to specific tabs. I can also use Ctrl + (PgUp / PgDn) to cycle through tabs (admittedly less convenient, but it can be remapped).

If I want to use vim tabs instead of gnome-terminal tabs, typing :tabn and :tabp is quite cumbersome. I could map them to keyboard shortcuts, but that is still a lot less convenient than jumping directly to tab 4 with Alt + 4.

Is there a faster way to switch between tabs in vim?

Matthew

Posted 2012-04-11T01:06:13.453

Reputation: 11 686

Answers

236

Next tab: gt

Prior tab: gT

Numbered tab: nnngt

ephemient

Posted 2012-04-11T01:06:13.453

Reputation: 20 750

2I found it very convenient to use '<' and '>' – Igor Stoppa – 2016-02-22T16:14:13.757

Scroll down for a better answer.. – The Vivandiere – 2016-09-09T18:09:30.167

1I know that I could type nnn g t, but I want to do it quickly, i.e. Alt + nnn or Ctrl + nnn. But Alt + n is already taken by gnome-terminal, and binding to Ctrl + n doesn't seem to have any effect. – Matthew – 2012-04-11T02:46:29.487

8What about mapping function keys to nnn g t? E.g., :map <F2> 2gt. There is the problem that <F1> is often mapped by GNOME to its help facility. Or you could choose some key you don't use often in normal mode, say the comma, and map it like this: :map , gt. Then 1, will take you to tab 1, 2, to tab 2, and so on. – garyjohn – 2012-04-11T05:00:18.363

46

Why not make use of your leader (my leader is mapped to Space):

" Go to tab by number
noremap <leader>1 1gt
noremap <leader>2 2gt
noremap <leader>3 3gt
noremap <leader>4 4gt
noremap <leader>5 5gt
noremap <leader>6 6gt
noremap <leader>7 7gt
noremap <leader>8 8gt
noremap <leader>9 9gt
noremap <leader>0 :tablast<cr>

You can use the settings below to toggle between the current and last active tab (here it is mapped to Ctrl+L, i.e., <c-l>):

" Go to last active tab

au TabLeave * let g:lasttab = tabpagenr()
nnoremap <silent> <c-l> :exe "tabn ".g:lasttab<cr>
vnoremap <silent> <c-l> :exe "tabn ".g:lasttab<cr>

unblevable

Posted 2012-04-11T01:06:13.453

Reputation: 561

2

You need au TabLeave * let g:lasttab = tabpagenr() for c-l to work: http://stackoverflow.com/questions/2119754/switch-to-last-active-tab-in-vim

– Ciro Santilli 新疆改造中心法轮功六四事件 – 2014-10-16T12:31:02.447

1I'd add <leader>h/l for gT and gt – Brenden – 2016-04-07T20:13:31.977

I think this answer is the most appropriate regarding the OP needs expressed. – Adrien – 2020-02-20T11:56:30.237

30

This is the easiest way that I found, to switch between tabs faster and simple.
Add next lines to your .vimrc and enjoy it, more tricks about vim tabs here.

nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>

Now you can use Ctrl to go left and Ctrl to go right.

Or just use:
1gt to go to tab one,
2gt to go to tab two,
3gt to go to tab three, etc... now you have the idea.

Arnold Gandarillas

Posted 2012-04-11T01:06:13.453

Reputation: 411

I think this is the best answer here. Very convenient to have. I just disabled those system keybindings in OS X for making them system independent. – Léo Léopold Hertz 준영 – 2016-05-14T05:47:05.383

2Use arrow keys will slow you down. I have them disabled – Ask and Learn – 2016-07-19T05:03:01.850

Yup... Best answer! – The Vivandiere – 2016-09-09T18:08:57.563

Sadly "Ctrl" gave me problems in vmplayer, so I changed to keys "Backspace" and "Enter". – Boris Däppen – 2017-07-13T11:30:50.360

Mac users: you may need to disable the default Mission Control spaces keyboard shortcuts: https://stackoverflow.com/questions/15719135/how-to-disable-keyboard-shortcuts-in-mac-os-x#answer-15719195

– azatar – 2017-08-29T22:52:29.017

@AskandLearn I think <C-Right> is a lot quicker than :tn repeated many times – Noel Evans – 2019-08-29T10:30:41.507

this should be the accepted answer – JDS – 2020-01-21T01:05:23.393

18

As I am on a Mac and not using MacVim (but plain vim within a terminal) I have had some difficulty with key combinations not being sent through to the terminal.

The most-compatible (and for me most comfortable) way to switch tabs quickly comes from the Vim Wikia site.

Place in your .vimrc file:

nnoremap H gT
nnoremap L gt

Now Shift-h (capital H) and Shift-l (capital L) will switch you quickly between tabs, and follows the convention that h and l correspond to left and right in vim on a regular qwerty keyboard.

Stefan Magnuson

Posted 2012-04-11T01:06:13.453

Reputation: 281

1I love this solution, intuitive and vim-like. No arrow keys needed. – Ivan Ivković – 2019-04-11T12:34:53.630

1I agree. A very nice solution – j sad – 2019-07-16T18:14:50.377

1this is really an intuitive solution. Thank you! – LeOn - Han Li – 2019-10-11T01:41:08.430

3

Add these to .vimrc to enable tab navigation hot keys:

<ctrl-l> toggle between 2 most recent tabs;

<ctrl-j/k> goto the last/next tab;

<ctrl-t> open a new tab.

" tab navigation: Alt or Ctrl+Shift may not work in terminal: " http://vim.wikia.com/wiki/Alternative_tab_navigation " Tab navigation like Firefox: only 'open new tab' works in terminal nnoremap <C-t> :tabnew<CR> inoremap <C-t> <Esc>:tabnew<CR> " move to the previous/next tabpage. nnoremap <C-j> gT nnoremap <C-k> gt " Go to last active tab au TabLeave * let g:lasttab = tabpagenr() nnoremap <silent> <c-l> :exe "tabn ".g:lasttab<cr> vnoremap <silent> <c-l> :exe "tabn ".g:lasttab<cr>

Mengqi Ji

Posted 2012-04-11T01:06:13.453

Reputation: 31

Is it possible to map <Ctrl>-<Tab> to the last toggle tab function. I've tried nnoremap <silent> <C-Tab> :exe "tabn ".g:lasttab<cr> and vnoremap <silent> <C-Tab> :exe "tabn ".g:lasttab<cr> without luck. – zzeroo – 2019-09-18T09:16:47.147

2

If you're using gvim or similar (i.e., something outside of the terminal), you can imitate the gnome-terminal behaviour you describe with:

" Map alt-x keys to jump to a tab
for i in range(1, 8)
  execute "nmap \<M-" . i . "> " . i . "gt"
endfor
nmap <M-9> :tablast<CR>

So alt-1 jumps to the first tab, etc. But alt-9 jumps to the last tab (as in Chrome).

You might want to wrap this in if has("gui_running") (although this doesn't seem to work with Neovim), or put this in .gvimrc.

Joe Freeman

Posted 2012-04-11T01:06:13.453

Reputation: 121

2

(Unfortunately) vim also uses CtrlPgDn/PgUp to cycle through tabs. You'll need to use map to map tabn/tabp to something usable.

Ignacio Vazquez-Abrams

Posted 2012-04-11T01:06:13.453

Reputation: 100 516