Linux

Use tabs to open multiple files in vim

Editing multiple files at once is made easier in the vim text editor with the use of tabs. Vincent Danen goes over the basic tab commands and shows you how to combine them with key bindings to make the most of a powerful editing tool.

Most people use a text editor to edit one file at a time. If more than one file needs to be edited at once, a GUI user can have multiple windows or tabs open with the files, but someone on the command-line will usually swap between different consoles or terminal windows. With vim, you can use tabs also, just like you would in gvim or any other GUI editor.

You can open files in multiple tabs in two ways. The first is to execute vim with each file specified:

$ vim -p file1.txt file2.txt file3.txt

This will open three tabs, each containing the specified file. The second way to open files is to create new tabs within vim and open files using :tabnew file.txt in command mode, which opens a new tab and edits the specified file (if you omit the filename, you will get a new empty document).

To close a tab, use :tabc. To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast). Finally, :tabs will give you a list of open tabs.

Each tab is a separate editing instance, which means that buffers are not shared between tabs. This also means that you can have multiple windows, or view ports, open in each tab (i.e., one file to edit on the top half of the screen, another file in the bottom half). For instance, if you have multiple tabs open and in one of them want to refer to vim's help system (i.e., :help tabs), you will have all of your tabs open, and in the foremost tab a split-screen editing view.

Using the various :tab* movement commands will still work, and using the [Ctrl]+W key bindings to manipulate windows (such as [Ctrl]+W then the down or up arrow to move between windows), will also work. Between tabs and windows, a single vim window can suddenly conveniently handle a large number of files at once.

Finally, to close a tab, close the file in it using :q or :wq if you need to save changes first.

Throw in some key bindings, and using tabs becomes even more convenient. For instance:

map <C-t><up> :tabr<cr>
map <C-t><down> :tabl<cr>
map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>

Adding the above to ~/.vimrc provides for a very convenient way to move between tabs. When in insert mode, press [Esc] to get to command mode, then use [Ctrl]+T plus a directional arrow to go to the tab you want: up to go to the first tab, down to the last, and left or right to go to the previous or next tab.

Using tabs is a great way to edit multiple text files in vim, without having to swap between terminals or consoles. Combining tab and window views, with some nice key bindings, makes it a simple, powerful, and very usable tool.

Delivered each Tuesday, TechRepublic's free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

5 comments
Russell S
Russell S

gt and gT are better ways for scroll through tabs, in my opinion. You won't interfere with the tag stack (assuming you're using it), and it's already there, no matter whose vim you're using

MarvinH
MarvinH

Thank you for this excellent primer on using tabs with vim. As an old-school vi user, I have not yet studied all of the features of vim. This article was a convenient way to learn a little more...

dirk.r.gently
dirk.r.gently

I always have my tab bar on so I know what I'm writing to :). In ~/.vimrc, i got: set showtabline=2 To select the tabs I enable vim mouse support: set mouse=ni

fevrin
fevrin

just the split functions to manage multiple files that I need to view simultaneously. I may look into using tabs, though, if I need different groups of files opened while not necessarily all viewable at once. Thanks for the tip--I was completely unaware this feature in vim until your article. There's just so much vim has to offer, it's astounding!

Editor's Picks