Skip to main content
Vim Text Editor:

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:

  1. Use gt to move to the next tab.
  2. Use :tabn to move to the next tab.

  3. Use gT to move to the previous tab.
  4. Use :tabp to move to the previous tab.

Go to specific tabs #

For moving to a specific tab, use the following shortcuts:

  1. Use 1gt to go to the first tab.
  2. Use :tabfirst to go to the first tab.
  3. Use 2gt to go to the second tab.

  4. Use :tabl to go to the last tab.
  5. Use :tablast to go to the last tab.

Close tabs #

If you want to close tabs, you can use the following shortcuts:

  1. Use :tabc to close the current tab.
  2. Use :tabclose to close the current tab.
  3. Use :tabclose 1 to close the first tab.
  4. Use :tabclose 2 to close the second tab.

  5. Use :tabo to close all but the current tab.
  6. 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:

  1. Use Shift + Tab to move to the previous tab.
  2. Use Tab to move to the next tab.
  3. Use Ctrl + t to open a new tab.
  4. 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:

  1. Use Shift + H to move to the previous tab.
  2. Use Shift + L to move to the next tab.

Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

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.

All Topics

Random Quote

“Perfection is finally attained not when there is no longer anything to add, but when there is no longer anything to take away.”

 Antoine de Saint-Exupery French writer, poet, and journalistWind, Sand and Stars, - IT quotes