274

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
  • 14,786
  • I'm using Cygwin's Gvim, and Ctrll+PgUp/PgDown works. Unfortunately, Ctrl+PgUp seems to be ignored a lot of the time unless I Alt+Tab to another application and Alt+Tab back. – user2153235 Apr 13 '23 at 22:49

9 Answers9

424

Next tab: gt

Prior tab: gT

Numbered tab: nnngt

ephemient
  • 25,184
  • 3
    I 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 Apr 11 '12 at 02:46
  • 12
    What 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 Apr 11 '12 at 05:00
  • 4
    I found it very convenient to use '<' and '>' – Igor Stoppa Feb 22 '16 at 16:14
  • 3
    Last tab: :tabl command (full form: :tablast). – Ruslan May 25 '20 at 17:06
82

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>
  • 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 OurBigBook.com Oct 16 '14 at 12:31
  • 2
    I'd add <leader>h/l for gT and gt – Brenden Apr 07 '16 at 20:13
  • I think this answer is the most appropriate regarding the OP needs expressed. – Adrien Feb 20 '20 at 11:56
  • Here is a lua equivalent for the last active tab. Sorry about the formatting: local lastTab = vim.api.nvim_create_augroup("LastTab", { clear = true }) vim.api.nvim_create_autocmd("TabLeave", { pattern = "*", command = "let g:lasttab = tabpagenr()", group = lastTab } ) vim.api.nvim_set_keymap('n', 'da', ':exe "tabn ".g:lasttab<CR>', {noremap = true, silent = true}) vim.api.nvim_set_keymap('v', 'da', ':exe "tabn ".g:lasttab<CR>', {noremap = true, silent = true}) – Fonnae Dec 04 '23 at 19:10
44

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.

36

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.

5

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

4

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>
phuclv
  • 27,773
  • Is it possible to map - 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 Sep 18 '19 at 09:16
3

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.

  • Lol I don't know why Although This is the Only answer that satisfies the question Yet the least up-voted one – Susano Mar 31 '21 at 19:20
  • 1
    @MahmoudSalah - it's likely b/c this Q&A comes up 1st on google and most ppl looking for this solution aren't on Linux. – slm May 29 '21 at 12:40
2

I like to switch windows/tabs with fzf-vim. It is fast and

For instance you can make a bindkey <leader>w which calls Windows or Buffers. Also Jumps and Changes are useful tools for fast switching.

Buffers:

enter image description here

Windows :

enter image description here

Here some maps for fzf I am using frequently:

nnoremap <silent><Leader>f :Files<CR>
nnoremap <silent><leader>j :Jumps<CR>
nnoremap <silent><leader>c :Changes<CR>
nnoremap <silent><Leader>m :Marks<CR>
nnoremap <silent><Leader>w :Windows<CR>
nnoremap <silent><Leader>h :Helptags<CR>
nnoremap <silent><Leader>b :Buffers<CR>
nnoremap <silent><Leader>bl :BLines<CR>
abu_bua
  • 429
  • :Windows (or :W) worked for me. Thanks! Fun fact: I have been accidentally pressing :W for years and immediately backing out without ever looking at what it actually did! – Max Coplan Jun 08 '23 at 15:17
1

I encountered this problem the same as you,
but later I find out what I really need might be a Marker.
Especially when using uppercase characters like mA ,
It will take you to your marked tab by using "`A" as fast as it can.
Hope it can help.