Search anything:

Open multiple files with Vim

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

VIM is one of the many Linux text editors. It is configurable and programmable. Usually, we open a single file with VIM. In this article, we demonstrate how to open multiple files in a single VIM session.

Table of contents.

  1. Introduction.
  2. Opening multiple files.
  3. Switching files.
  4. Managing buffers.
  5. Saving changes.
  6. Quitting VIM.
  7. VIM tabs.
  8. Copying files.
  9. Summary.
  10. References.

Opening multiple files.

A basic example, is the following, to open two files, file1.txt and file2.txt we write:

vim file1.txt file2.txt

Switching files.

When we execute the above command, we will only be able to operate on a single file at a time, in this case, file1.txt, to switch to another file we write:


The above commands move forward, that is after the first file we go to the second then the third until there are no more left. When the last is reached and error is shown:

We can also use :next and :prev for switching back and forth between open files in vim.

To switch backward, we write:



We can also skip the middle files and switch between the last and first files, this is assuming we have more than three files. In such a case we write:


To switch to the first file.
And to switch to the last file we write:



We can also be specific about the exact file we want to switch to by using numbers, for example, to switch to the third file we write:


And to switch to the 7th file we write:



Managing buffers.

Within VIM while editing a file, we can also decide to open a file that was not previously opened, for this we need to know its filename. For example to open a file file.txt that was not previously opened we write:

:e filename

We can also add a new buffer by writing:

:badd [file name]

We can list buffers by writing:


To delete the current buffer we write:



Saving changes.

After editing files and switching between them we may forget to write changes, to save changes across all files, we write:


To save changes in the current file and move to the next we write:


To discard changes in the current file and move to the previous file we write:


Quitting VIM.

After saving changes, what remains is quitting and exiting vim. We can either quit a single file or all at once.

To quit the current file we write:


And to quit all currently opened files, we write:



Sometimes we don't want to save changes and also want to avoid the warnings that occur when we don't save. In such a case we need to forcefully quit a file. To quit all currently open files forcefully, we write:


VIM tabs.

Apart from being able to open files in a single buffer and switching between them, we can open vim files in tabs by using the -p option. For example to open two files file1.txt and file2.txt in tabs we write:

vim -p file1.txt file2.txt

Once open we can switch to the next tab by pressing escape(normal mode) and typing gt or typing :tabn or :tabnext.

To switch to previous tabs we press gt while in the normal mode or type :tabp or :tabprevious in the command line mode.

We can also press CTRL+^ as a shortcut.

If we have multiple tabs, typing the above two commands can be repetitive, to jump to a specific tab, we write:


In the above command n represents the tab number, for example, to jump to tab number 7 we write:


Note that we have to be in normal mode for this to work. We can enter normal mode by pressing the escape key.

We don't even need to remember a file number, we can list currently open files by writing:


From the output, % represents the current file while # is the alternative.

When done with a tab, we close it by writing:

# OR

As an alternative, we can also use a mouse to switch between different tabs. For this we write:

:set mouse=a

After this the mouse will be enabled and we can use it to select different tabs.

Copying files.

What about copying? We may want to copy contents in one file into another. For example to copy a line from a file file1.txt to another file file2.txt we first switch to file1.txt buffer:

:buffer 1

Then yank the line we want to copy, to yank is to copy, for this, we move the cursor to the line we intent to copy then press letter y twice. After this operation we switch to file2.txt buffer and press p to paste the copied line.

We can also copy the entire file to another different file. For example to copy all the contents of file1.txt to file2.txt, we first open file2.txt then while there execute the command:

:r file1.txt


Here r means read. Contents of file1.txt are pasted into file2.txt after we press enter key.


Vim is a very powerful text editor that offers amazing features out of the box without needing to install plugins.

We learned about opening multiple files in buffers and tabs and switching between them. We also learned how to copy content across open files and tabs.


For more information about VIM usage, we can execute the command $ man vim in a Linux terminal.

Open multiple files with Vim
Share this