How to switch between tabs in Vim
Summary
Learn how to switch between tabs in Vim using commands like gt
, gT
, 1gt
, and 2gt
. The article also discusses how to customize tab navigation by remapping keys to, e.g. Ctrl+Tab or Ctrl+Shirft+Tab, in .vimrc
for smoother tab navigation.
Introduction #
Vim is a highly efficient and customizable text editor that supports multiple tabs, allowing to work on different files simultaneously. This article explains how to navigate, or switch, between tabs.
Tab navigation with built-in keybindings #
Vim provides a set of built-in commands to navigate between tabs.
Next and previous tab #
For moving from one tab to the next, use the following shortcuts:
- Use
gt
to move to the next tab. Use
:tabn
to move to the next tab.- Use
gT
to move to the previous tab. - Use
:tabp
to move to the previous tab.
Go to specific tabs #
For moving to a specific tab, use the following shortcuts:
- Use
1gt
to go to the first tab. - Use
:tabfirst
to go to the first tab. Use
2gt
to go to the second tab.- Use
:tabl
to go to the last tab. - Use
:tablast
to go to the last tab.
Close tabs #
If you want to close tabs, you can use the following shortcuts:
- Use
:tabc
to close the current tab. - Use
:tabclose
to close the current tab. - Use
:tabclose 1
to close the first tab. Use
:tabclose 2
to close the second tab.- Use
:tabo
to close all but the current tab. - Use
:tabonly
to cluse all but the current tab.
By using these commands, you can navigate between multiple open tabs without relying on additional plugins or configurations.
Remapping tab navigation in .vimrc
#
If you want to make tab navigation even easier, you can create custom key mappings in your .vimrc
file. The .vimrc
file is a configuration file for Vim where you can define custom settings and key bindings. The file usually resides in your user’s root directory, ~/
.
Regular style #
If you like Vim to work similar to your, e.g. web browser, add the following lines to your .vimrc
file:
" tab navigation
nnoremap <S-Tab> :tabprevious<CR>
nnoremap <Tab> :tabnext<CR>
nnoremap <C-t> :tabnew<CR>
nnoremap <C-w> :tabclose<CR>
These keybinding mappings allow you to:
- Use
Shift + Tab
to move to the previous tab. - Use
Tab
to move to the next tab. - Use
Ctrl + t
to open a new tab. - Use
Ctrl + w
to close the current tab.
With these remappings, you can navigate tabs more intuitively using familiar key combinations.
Vim style #
If your fingers are trained for Vim navigation, you could also try these mappings:
nnoremap H gT
nnoremap L gt
These keybinding mappings allow you to:
- Use
Shift + H
to move to the previous tab. - Use
Shift + L
to move to the next tab.
Further readings #
Sources and recommended, further resources on the topic:
License
How to switch between tabs in Vim by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/how-to-switch-between-tabs-in-vim">How to switch between tabs in Vim</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the Ditig legal page.