3 Ways to Use Tabs in Vim

Dayne Dayne (57)
5 minutes

Vim tabs can be a great way to visualize which files you currently have open. Many vim users get bogged down by them and either end up working too hard to use them or give up altogether. Here are 3 great ways to incorporate tabs into your workflow.

Posted in these interests:
h/vim23 guides

The most obvious way to think of tabs is like any other tabbed editor or web browser. Each time you open another file, you open it in a new tab. The most relevant commands for this style of usage are as follows:

In normal mode:

gt            go to next tab
gT            go to previous tab
{i}gt         go to tab in position i

In command mode

:tabn         go to next tab
:tabp         go to previous tab
:tabfirst     go to first tab
:tablast      go to last tab

:tabm 0 move current tab to first :tabm move current tab to last :tabm {i} move current tab to position i+1

You can and should make your own mappings to make these more convenient. Here's a good start to help you quickly jump to any tab:

nnoremap <Leader>1 1gt
nnoremap <Leader>2 2gt
nnoremap <Leader>3 3gt
nnoremap <Leader>4 4gt
nnoremap <Leader>5 5gt
nnoremap <Leader>6 6gt
nnoremap <Leader>7 7gt
nnoremap <Leader>8 8gt
nnoremap <Leader>9 9gt

Vim tabs, windows, and buffers
A vim tab containing 3 windows, each editing a different buffer

According to vim's documentation:

Summary:
   A buffer is the in-memory text of a file.
   A window is a viewport on a buffer.
   A tab page is a collection of windows.

If we adopt this view, then using tabs as our primary way of visualizing which files are open is a little bit flawed. Here's how we can improve.

We can begin to think about the files we have open as our buffers (you can see all currently open buffers using the :buffers command). Then, we can use windows to our advantage by using vim splits:

" create horizontal split
:sp
" create vertical split
:vsp

Then, we can organize our windows within tabs. Tabs are now treated as they should be, as a collection of windows.

If vim's buffer, window, tab paradigm is too difficult to work with or just doesn't work for your brain, there are many alternative ways to think about it. Many people forego native tabs altogether and use something like airline to print all open buffers at the top of vim. Combined with something like buffergator, this allows you to see all buffers and quickly switch to any of them using a visual selection.

Personally, I have found my zen somewhere in between. I use buffergator to switch through buffers but I don't use airline to print them at the top like tabs. When I have a specific need for a layout other than just a single window, I'll reach for a new tab and get to work.

John John (304)
2 minutes

Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.