Vim 101: Tabs

When Vim 7.0 was released, a tabbed interface was introduced. It works in both the text mode and GUI versions of Vim, but it doesn't take up any extra interface space if it isn't used.

Tabs are actually known as a "tab page". The help file for tab pages can be accessed with :help tabpage. Tabs can hold multiple split windows, so it's very easy to get into a complete mess of weird and wonderful window layouts.

Creating and Closing Tabs

When opening Vim, vim -p file1 file2 can be used to open several files, each with their own tab.

From within Vim, a new tab can be created with :tabnew, or :tabe which is short for "tabedit". These commands accept a file argument.

Tabs can be closed with :tabc, which is short for "tabclose". Additionally, :tabc! will close even if the buffer has changes. Typing :tabc 3 will close the third tab.

Navigating Between Tabs

To move to the next or previous tabs, use :tabn and :tabp -- these are short for "tab next" and "tab previous" so they're easy to remember. Typing gt and gT will also switch to the next and previous tabs, and I find myself using these more than the Ex commands.

Typing :tabs shows a list of tabs.


Executing Commands

Typing :tabdo {cmd} will execute {cmd} in each tab page. If you've opened a set of related files in several tabs and want to run a search and replace on them, then :tabdo is the command you're looking for. It can be shortened to :tabd, so for a search and replace the following could be used:

:tabd %s/Rake/Make/ec

Here I've used ec with the regular expression, which causes Vim to confirm before making a replacement, and ignore certain errors (like "pattern not found"). Notice how similar this is to :argdo.

blog comments powered by Disqus