Home Vim Tips How To Edit Multiple Files Using Vim Editor

How To Edit Multiple Files Using Vim Editor

By sk
Published: Updated: 23.3K views

Sometimes, you will find yourself in a situation where you want to make changes in multiple files. Or you might want to copy the contents of one file to another. If you're on GUI mode, you could simply open the files in any graphical text editor, like gedit, and use CTRL+C and CTRL+V to copy/paste the contents. In CLI mode, you can't use such editors. No worries!Β  Where there is vim editor, there is a way! In this tutorial, we are going to learn to edit multiple files at the same time using Vim editor. Trust me, this is very interesting Vim feature!

Install Vim On Linux

Vim editor is available in the official repositories of most Linux distributions. So you can install it using the default package manager.

For example, on Arch Linux and its variants you can install it using command:

$ sudo pacman -S vim

On Debian, Ubuntu:

$ sudo apt-get install vim

On RHEL, CentOS:

$ sudo yum install vim

On Fedora:

$ sudo dnf install vim

On openSUSE:

$ sudo zypper install vim

Edit multiple files at a time using Vim editor in Linux

There could be many ways to edit multiples with Vim editor. I know two methods.

Method 1

I have two files namely file1.txt and file2.txt, with a bunch of random words. Let us have a look at them.

$ cat file1.txt 
ostechnix
open source
technology
linux
unix
$ cat file2.txt 
line1
line2
line3
line4
line5

Now, let us edit these two files at a time using Vim editor. To do so, run:

$ vim file1.txt file2.txt

Vim will display the contents of the files in an order. The first file's contents will be shown first and then second file and so on.

Edit Multiple Files Using Vim Editor

Edit Multiple Files Using Vim Editor

Switch between files

To move to the next file, type:

:n
Switch between files in Vim editor

Switch between files in Vim editor

To go back to previous file, type:

:N

Here, N is capital (Type SHIFT+n).

Start editing the files as the way you do with Vim editor. Press 'i' to switch to interactive mode and modify the contents as per your liking. Once done, press ESC to go back to normal mode.

Vim won't allow you to move to the next file if there are any unsaved changes. To save the changes in the current file, type:

ZZ

Please note that it is double capital letters ZZ (SHIFT+zz).

To abandon the changes and move to the previous file, type:

:N!

To view the files which are being currently edited, type:

:buffers
View files in buffer in VIm

View files in buffer in VIm

You will see the list of loaded files at the bottom.

List of files in buffer in Vim

List of files in buffer in Vim

To switch to the next file, type :buffer followed by the buffer number. For example, to switch to the first file, type:

:buffer 1

Or, just do:

:b 1
Switch to next file in Vim

Switch to next file in Vim

Just remember these commands to easily switch between buffers:

:bf            # Go to first file.
:bl            # Go to last file
:bn            # Go to next file.
:bp            # Go to previous file.
:b number  # Go to n'th file (E.g :b 2)
:bw            # Close current file.

Opening additional files for editing

We are currently editing two files namely file1.txt, file2.txt. You might want to open another file named file3.txt for editing. What will you do? It's easy! Just type :e followed by the file name like below.

:e file3.txt
Open additional files for editing in Vim

Open additional files for editing in Vim

Now you can edit file3.txt.

To view how many files are being edited currently, type:

:buffers
View all files in buffers in Vim

View all files in buffers in Vim

Please note that you can not switch between opened files with :e using either :n or :N. To switch to another file, type :buffer followed by the file buffer number.

Copying contents of one file into another

You know how to open and edit multiple files at the same time. Sometimes, you might want to copy the contents of one file into another. It is possible too. Switch to a file of your choice. For example, let us say you want to copy the contents of file1.txt into file2.txt.

To do so, first switch to file1.txt:

:buffer 1

Place the move cursor in-front of a line that wants to copy and type yy to yank(copy) the line. Then, move to file2.txt:

:buffer 2

Place the mouse cursor where you want to paste the copied lines from file1.txt and type p. For example, you want to paste the copied line between line2 and line3. To do so, put the mouse cursor before line and type p.

Sample output:

line1
line2
ostechnix
line3
line4
line5
Copying contents of one file into another file using Vim

Copying contents of one file into another file using Vim

To save the changes made in the current file, type:

ZZ

Again, please note that this is double capital ZZ (SHIFT+z).

To save the changes in all files and exit vim editor. type:

:wq

Similarly, you can copy any line from any file to other files.

Copying entire file contents into another

We know how to copy a single line. What about the entire file contents? That's also possible. Let us say, you want to copy the entire contents of file1.txt into file2.txt.

To do so, open the file2.txt first:

$ vim file2.txt

If the files are already loaded, you can switch to file2.txt by typing:

:buffer 2

Move the cursor to the place where you wanted to copy the contents of file1.txt. I want to copy the contents of file1.txt after line5 in file2.txt, so I moved the cursor to line 5. Then, type the following command and hit ENTER key:

:r file1.txt
Copying entire contents of a file into another file

Copying entire contents of a file into another file

Here, r means read.

Now you will see the contents of file1.txt is pasted after line5 in file2.txt.

line1
line2
line3
line4
line5
ostechnix
open source
technology
linux
unix
Copying entire file contents into another file using Vim

Copying entire file contents into another file using Vim

To save the changes in the current file, type:

ZZ

To save all changes in all loaded files and exit vim editor, type:

:wq

Method 2

The another method to open multiple files at once is by using either -o or -O flags.

To open multiple files in horizontal windows, run:

$ vim -o file1.txt file2.txt
Open multiple files at once in Vim

Open multiple files at once in Vim

To switch between windows, press CTRL-w w (i.e Press CTRL+w and again press w). Or, use the following shortcuts to move between windows.

  • CTRL-w k - top window
  • CTRL-w j - bottom window

To open multiple files in vertical windows, run:

$ vim -O file1.txt file2.txt file3.txt
Open multiple files in vertical windows in Vim

Open multiple files in vertical windows in Vim

To switch between windows, press CTRL-w w (i.e Press CTRL+w and again press w). Or, use the following shortcuts to move between windows.

  • CTRL-w l - left window
  • CTRL-w h - right window

Everything else is same as described in method 1.

For example, to list currently loaded files, run:

:buffers

To switch between files:

:buffer 1

To open an additional file, type:

:e file3.txt

To copy entire contents of a file into another:

:r file1.txt

The only difference in method 2 is once you saved the changes in the current file using ZZ, the file will automatically close itself. Also, you need to close the files one by one by typing :wq. But, had you followed the method 1, when typing :wq all changes will be saved in all files and all files will be closed at once.

For more details, refer man pages.

$ man vim

Suggested read:


You know now how to edit multiples files using vim editor in Linux. As you can see, editing multiple files is not that difficult. Vim editor has more powerful features.

We will write more Vim tips in the days to come. Stay tuned!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

3 comments

Azrael March 18, 2018 - 4:14 am

Talking about multiple file without invoking the :Sex command, shame on you πŸ˜‰

Reply
dufy March 19, 2018 - 7:07 pm

“Vim won’t allow you to move to the next file if there are any unsaved changes.”
In your .vimrc save setting: set hidden

“To save the changes in the current file, type: ZZ”
Better is to use ex command: :write or shorter :w

Reply
Pavlin Georgiev October 9, 2020 - 6:18 pm

Very helpful article on VIM.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More