Sometimes you may need to modify multiple files or copy the contents of one file to another. In the graphical user interface, you can open the file in any graphical text editor (such as gedit) and use CTRL + C and CTRL + V to copy and paste the content. You cannot use this editor in command line mode. But don't worry, as long as you have a vim editor. In this tutorial, we will learn to use the vim editor to edit multiple files at the same time. Believe me, it's very interesting.
Install Vim
The Vim editor can be found in the official depots of most Linux distributions, so you can install it with the default package manager. For example, on Arch Linux and its variants, you can use the following command:
$ sudo pacman -S vim
On Debian and Ubuntu:
$ sudo apt-get install vim
On RHEL and CentOS:
$ sudo yum install vim
On Fedora:
$ sudo dnf install vim
On openSUSE:
$ sudo zypper install vim
Edit multiple files simultaneously using Linux's Vim editor
Now let's talk about business, we can do this in two ways.
method one
There are two files, file1.txt and file2.txt, with a bunch of random words:
$ cat file1.txt
Ostechnix
Open source
Technology
Linux
Unix
$ cat file2.txt
Line1
Line2
Line3
Line4
Line5
Now let's edit both files at the same time. Please run:
$ vim file1.txt file2.txt
Vim will display the contents of the file in order. The content of the first file is displayed first, then the second file is displayed, and so on.
- Switch in file
To move to the next file, type:
:n
To return to the previous file, type:
:N
If there are any unsaved changes, Vim will not allow you to move to the next file. To save the changes in the current file, type:
ZZ
Please note that there are two uppercase letters ZZ (SHIFT + zz).
To discard the changes and move to the previous file, type:
:N!
To see the file currently being edited, type:
:buffers
You will see the list of loaded files at the bottom.
To switch to the next file, enter :buffer followed by the buffer number. For example, to switch to the first file, type:
:buffer 1
- Open other files for editing
We are currently editing two files, file1.txt and file2.txt. I want to open another file named file3.txt for editing.
What would you do? This is easy. Just type :e and enter the file name as shown below:
:e file3.txt
Now you can edit file3.txt.
To see the number of files currently being edited, type:
:buffers
Please note that you cannot switch using :n or :N for files opened with :e. To switch to another file, enter :buffer and then enter the file buffer number.
- Copy the contents of one file to another
You already know how to open and edit multiple files at the same time. Sometimes you may want to copy the contents of one file to another. This can also be done. Switch to the file of your choice, for example, suppose you want to copy the contents of file1.txt to file2.txt:
First, switch to file1.txt:
:buffer 1
Move the cursor to the front of the row you want to copy, and type yy to extract (copy) the row. Then move to file2.txt:
:buffer 2
Move the cursor to the location where you want to paste the copy line from file1.txt and type p. For example, if you want to paste the copied line between line2 and line3, place the mouse cursor before the line and type p.
Sample output:
Line1
Line2
Ostechnix
Line3
Line4
Line5
To save the changes made in the current file, type:
ZZ
Again, there are two capital letters ZZ (SHIFT + z).
Save all file changes and exit the vim editor, type:
:wq
Similarly, you can copy any line of any file to other files.
- Copy entire file content to another file
We know how to copy a line, then the content of the entire file? it is also fine. For example, you want to copy the entire contents of file1.txt to file2.txt.
Open file2.txt first:
$ vim file2.txt
If the file is already loaded, you can switch to file2.txt by entering the following command:
:buffer 2
Move the cursor to the location where you want to paste the contents of file1.txt. I want to paste the contents of file1.txt after the fifth line of file2.txt, so I move the cursor to line 5. Then, type the following command and press Enter:
:r file1.txt
Here, r stands for "read".
You will now see the contents of file1.txt pasted on line 5 of file2.txt.
Line1
Line2
Line3
Line4
Line5
Ostechnix
Open source
Technology
Linux
Unix
To save the changes in the current file, type:
ZZ
To save all changes for all files and exit the vim editor, enter:
:wq
Method Two
Another way to open multiple files at the same time is to use the -o or -O flag.
To open multiple files in a horizontal window, run:
$ vim -o file1.txt file2.txt
To switch between windows, press CTRL-w (press CTRL + w and press w again). Or, you can use the following shortcuts to move between windows:
CTRL-w k - above window
CTRL-w j - the following window
To open multiple files in a vertical window, run:
$ vim -O file1.txt file2.txt file3.txt
To switch between windows, press CTRL-w (press CTRL + w and press w again). Or, use the following shortcuts to move between windows:
CTRL-w l - left window
CTRL-w h - right window
Everything else is the same as the description of method one.
For example, to list the currently loaded files, run:
:buffers
Switch between files:
:buffer 1
Open another file, type:
:e file3.txt
Copy the entire contents of the file to another file:
:r file1.txt
The only difference in method two is that as long as you use ZZ to save changes to the current file, the file is automatically closed. Then, you need to type :wq in order to close the file. However, if you follow method one, entering :wq will save all changes in all files and all files will be closed immediately.
Ftta,Ftta Fiber,Fibre Ftta,Fibre Optique Ftta
Huizhou Fibercan Industrial Co.Ltd , https://www.fibercan-network.com