Skip to main content

Other Vim Tips

Using Tabbed Interface in Vim

Give Vim and IDE touch by using tabs to open multiple files. Yes, the same tab experience you have in a web browser or a regular text editor.

Warp Terminal

Think of Vim tabs like browser tabs for your code editor - each tab holds one or more windows, letting you organize multiple files into logical workspaces.

Unlike window splits that divide your screen, tabs stack contexts you can flip between instantly.

Three files opened in separate tabs in Vim
Three files opened in separate tabs in Vim

Let's see how you can use tabs in Vim.

Essential Vim tab commands at a glance

Here are the most common actions you can use while dealing with tabs in Vim.

Command Action Memory Hook
vim -p file1 file2 Opens files in tabs Vim in pages
:tabnew filename Open file in new tab Tab new
:tabedit filename Open file for editing in new tab Tab edit
gt Next tab Go to next
gT Previous tab Go to previous
{n}gt Jump to tab number n Go to specific
:tabfirst Jump to first tab Self-explanatory
:tabclast Jump to last tab Self-explanatory
:tabclose Close current tab Self-explanatory
:tabonly Close all other tabs Keep only this
:tabs List all tabs Show tabs

Interesting, right? Let's see it in details.

Opening files in tabs in Vim

Let's start by opening files in tabs first.

Start Vim with multiple files opened in tabs

Launch Vim with multiple tabs instantly:

vim -p file1.py file2.py file3.py
0:00
/0:13

Open two existing files in tabs while starting Vim

How can you open just one file in tab? Well... if it's one file, what's the point of tab, right?

📋
Vim tabs aren't file containers - they're viewport organizers. Each tab can hold multiple split windows, making tabs perfect for grouping related files by project, feature, or context. It's like having separate desks for different projects.

Open a file in a new tab in the current Vim session

When you are already inside Vim and want to open a file in a new tab, switch to normal mode by pressing Esc key and use the command:

:tabnew filename

This will load the file in a new tab. If the file doesn't exist, it will create a new one.

Filename is optional. If you don't provide it, it will open a new file without any name:

:tabnew
0:00
/0:11

Opening existing or new files in tabs from existing Vim session

💡
If you use tabedit instead of tabnew, it open the file in Edit mode (insert mode) in the new tab.

Search for files and open them in tabs

Search the current directory for filename matching the given pattern and open it in a new tab:

:tabf filename*

This only works if the search results into a single file. If there are more than one file matched, it will throw an error:

E77: Too many file names
💡
While you can open as many tabs as you want, only 10 tabs are shown by default. You can change this by setting tabpagemax in your vimrc to something like set tabpagemax=12

You can move between opened tabs using:

  • :tabn: for next tab
  • :tabp: for previous tab

Typing the commands could be tedious, so you can use the following key combinations in the nomral mode:

  • gt: To go to the next tab
  • gT (i.e. press g and shift and t keys together) To go to the previous tab

If there are too many tabs opened, you can use:

  • :tabfirst: Jump to first tab
  • :tablast: Jump to last tab
💡
You can enable mouse mode in Vim and that makes navigating between tabs easier with mouse clicks.

In many distributions these days, Vim is preconfigured to show the tab labels on the top. If that's not the case, add this to your vimrc:

set showtabline=2

You can list all the opened tabs with:

:tabs
Using :tabs shows all the opened tabs details
💡
If you are particular about putting the opened tabs in specific order, you can move the current tab to Nth position with :tabm N. This tabm is short for tabmove. Note that Vim starts numbering at 0.

Closing tabs

How do you close a tab? If the tab has a single filed opened, the regular save/exit Vim commands work.

But it will be an issue if you have multiple split windows opened in a tab.

  • :tabclose: Close current tab
  • :tabonly: Only keep the current tab opened, close all others
0:00
/0:14

Tab closing operation in Vim

💡
Accidentally closed a tab? :tabnew | u creates a new tab and undoes in one motion - your file returns.

Bulk tab operations

With :tabdo command, you can run the same operations in all the tabs.

For example, :tabdo w will save file changes in all tabs, :tabdo normal! gg=G auto-indents every file.

Similarly, tabdo %s/oldvar/newvar/g executes search-replace across every tab simultaneously. Parallel processing for repetitive changes.

You get the idea. tabdo is the key here.

💡
You can save your meticulously crafted tab layout. :mksession project.vim then vim -S project.vim restores exact tab layout.

Conclusion

While it is good to enjoy tabs in Vim, don't create dozens of them - they become harder to navigate than helpful. Use buffers for file switching and tabs for context switching.

As you can see, with the tab feature, you get one inch closer to having the IDE like experience in Vim.

Abhishek Prakash