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!
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.