In vim, how can I quickly switch between tabs?
Solution 1
Next tab: gt
Prior tab: gT
Numbered tab: nnngt
Solution 2
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>
Solution 3
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.
Solution 4
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.
Solution 5
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>
Related videos on Youtube
cukabeka
Updated on September 18, 2022Comments
-
cukabeka over 1 year
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?
-
cukabeka about 12 yearsI know that I could type
nnn
g
t
, but I want to do it quickly, i.e.Alt + nnn
orCtrl + nnn
. ButAlt + n
is already taken by gnome-terminal, and binding toCtrl + n
doesn't seem to have any effect. -
garyjohn about 12 yearsWhat 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
. Then1,
will take you to tab 1,2,
to tab 2, and so on. -
Ciro Santilli Путлер Капут 六四事 over 9 yearsYou need
au TabLeave * let g:lasttab = tabpagenr()
forc-l
to work: stackoverflow.com/questions/2119754/… -
Igor Stoppa about 8 yearsI found it very convenient to use '<' and '>'
-
Brenden about 8 yearsI'd add
<leader>h/l
forgT
andgt
-
indi almost 8 yearsI 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.
-
Ask and Learn almost 8 yearsUse arrow keys will slow you down. I have them disabled
-
The Vivandiere over 7 yearsYup... Best answer!
-
Boris Däppen almost 7 yearsSadly "Ctrl" gave me problems in vmplayer, so I changed to keys "Backspace" and "Enter".
-
4Z4T4R over 6 yearsMac users: you may need to disable the default Mission Control spaces keyboard shortcuts: stackoverflow.com/questions/15719135/…
-
Glorious Kale about 5 yearsI love this solution, intuitive and vim-like. No arrow keys needed.
-
j sad almost 5 yearsI agree. A very nice solution
-
Noel Evans over 4 years@AskandLearn I think <C-Right> is a lot quicker than :tn repeated many times
-
The-Duck over 4 yearsIs it possible to map <Ctrl>-<Tab> to the last toggle tab function. I've tried
nnoremap <silent> <C-Tab> :exe "tabn ".g:lasttab<cr>
andvnoremap <silent> <C-Tab> :exe "tabn ".g:lasttab<cr>
without luck. -
LeOn - Han Li over 4 yearsthis is really an intuitive solution. Thank you!
-
JDS over 4 yearsthis should be the accepted answer
-
Adrien about 4 yearsI think this answer is the most appropriate regarding the OP needs expressed.
-
Ruslan almost 4 yearsLast tab:
:tabl
command (full form::tablast
). -
Grijesh Chauhan almost 4 yearsOn Mac it worked for me with
command
+Ctrl
+arrow-keys
-
Mahmoud Salah about 3 yearsLol I don't know why Although This is the Only answer that satisfies the question Yet the least up-voted one
-
slm almost 3 years@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.