Vim Recipes ‣ Navigation ‣ Navigating Tabs
You want to group your windows into multiple, logical groups.
When you have a lot of open windows/buffers it can be tricky to navigate between them. Often it makes more sense to group them into logical tabs, so you can switch between them easier, and operate on them as a group.
For example, if you were using Vim to edit a website, you may have your CSS files open in one tab, the HTML files in another, and a HTML reference guide in the third.
Use tabs.
| Command | Action |
|---|---|
| :tabedit [file] | Open a new tab. If the optional file is supplied, that file is opened in the new tab. |
| :tabclose | Close the current tab. |
| :tabnext n | Go to next tab, or the nth |
| ngt | |
| :tabs | Show a list of the open tabs. |
| :tabprevious n | Go to previous tab, or the nth |
| ngT | |
| :tabdo cmd | Executes cmd in each open tab, aborting on the first error. |
You can use Vim's tabs like those in Firefox® and Opera®, by opening one file in each tab, then switching between them. Vim enables you to extend this concept, however, by allowing multiple files to be opened in the same tab.
When you open a tab, a tabline appears along the top of the screen, which lists the open tabs. In GVim you can switch to another tab, close existing tabs, and open new tabs, by clicking the tabline with the mouse.
You can either cycle through open tabs using gt, or go directly to a specific tab by prefixing gt with its number. Tabs are numbered starting with 1, so to switch to the 3rd tab on the tabline, say, you'd use 3gt. If you have a lot of tabs, their numbers may not be obvious. In this case, use :tabs to find them.
The power of tabs comes from executing commands on the windows they contain as a logical group. Continuing the above example, this would let you perform a search and replace all HTML files. For example, if you were in the HTML tab, you could say :tabdo s/<foo>/<bar>/g, and all of your HTML files would have their <foo>s replaced with <bar>s.