3 ways to use tabs in vim

Dayne Dayne (56)
0

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:

vim
PRIMARY
23 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.

Dayne's profile pictureDayne
Joined in 2015
Software engineer, co-founder of Howchoo, and renaissance man. Lifelong amateur woodworker, espresso mechanic, freestyle lyricist, drummer, artist, runner, coffee roaster, electrical engineer, gamer, inventor, churner, psychoanalyst, photographer, pizza chef, pit master, audiophile, guitarist, entrepreneur, dad, yogi, cyclist, and barista.
Share this guide!
Related to this guide:
Vim undo branches
You probably already know how to use vim's basic undo and redo features. For a quick refresher, read this short tutorial on how to use vim redo and undo.
Dayne's profile picture DayneView
In these interests: vim
Vim redo and undo
Learn how to undo and redo changes using vim. If you are brand new to vim, learn more about the basics in this intro to vim guide.
Dayne's profile picture DayneView
In these interests: vim
Crash course in vim text objects
Once you learn to think in text objects, you'll never be the same
Dayne's profile picture DayneView
In these interests: vim
People also read:
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines.
One of the strengths of Vim is that it's highly customizable. And while remapping keys is possible, we often want to create shortcuts without changing the default key bindings.
If you find yourself highlighting text in Vim with your mouse to copy and paste it elsewhere, stop. There's a better way, using the yank command, to copy text into your clipboard on macOS or Windows.
Basic vim commands
Because you've accidentally opened Vim and don't know how to exit
Vim is amazing. Well, Python is doing all the work, but I still like Vim. Learn how to format JSON in Vim like a pro with this short guide! tl;dr Paste your json, and run this in vim normal mode. :%!
Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.
Need to change the case of characters to all caps or all lowercase? This is easily done using Vim. This guide will show you how to change the case of characters in Vim.
Posted in these interests:
vim
vim
23 guides 16 subscribers 
PRIMARY
Explore
Discuss this guide: