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:tabl (tablast). Finally, :tabs will give you a list of open tabs. (tabrewind) and to jump to the last tab use
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 :tabr

map :tabl

map :tabp

map :tabn

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.

No comments:

Post a Comment