Open Source

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:


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


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:


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.




Editor's Picks