In vim, how can I quickly switch between tabs?
Next tab: gt
Prior tab: gT
Numbered tab: nnngt
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>
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.
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.
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>