×

Search anything:

Open multiple files with Vim

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

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:

:n
#or
:bn

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:

:N 
#OR
:bp

tty-1

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:

:bf

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

:bl

tty1-1

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:

:b3

And to switch to the 7th file we write:

:b7

tty2-1

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:

:buffers

To delete the current buffer we write:

:bdelete

tty3-1

Saving changes.

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

:wall

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

:ZZ

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

:N!

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:

:bw

And to quit all currently opened files, we write:

:qall

tty4

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:

:qall!

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:

ngt

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

7gt

tty5
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:

:ls

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

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

:tabc
# OR
:tabclose

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

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

tty6

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

Summary.

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.

References.

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

Open multiple files with Vim
Share this