Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Vim editor to edit multiple files

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use the Vim editor to edit multiple files, the article is very detailed, has a certain reference value, interested friends must read it!

Install Vim

The Vim editor can be found in the official software repository 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 uses Linux's Vim editor to edit multiple files simultaneously

Now let's get down to 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.txtostechnixopen sourcetechnologylinuxunix $cat file2.txtline1line2line3line4line5

Now let's edit both files at the same time. Please run:

$vim file1.txt file2.txt

Vim displays the contents of the file in order. First display the contents of * * files, then display the second file, and so on.

Switch in a 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 changes in the current file, type:

ZZ

Notice that it is two capital letters ZZ (SHIFT + zz).

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

: N!

To view the file currently being edited, type:

: buffers

You will see a 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 * files, 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 called file3.txt for editing.

What would you do? It's easy. Simply type: e, and then enter the file name as follows:

: e file3.txt

Now you can edit file3.txt.

To view the number of files currently being edited, type:

: buffers

Note that for files opened with: e, you cannot switch using: n or: N. 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 in front of the line you want to copy, and type yy to extract (copy) the line. Then, move to file2.txt:

: buffer 2

Move the cursor to the location where you want to copy the row from the file1.txt paste, and type p. For example, if you want to paste the copied line between line2 and line3, place the mouse cursor in front of the line and type p.

Sample output:

Line1line2ostechnixline3line4line5

To save changes in the current file, type:

ZZ

Again, there are two capital letters ZZ (SHIFT + z).

Save changes to all files and exit the vim editor, type:

: wq

Similarly, you can copy any line of any file to another file.

Copy the entire file contents to another file

We know how to copy a line, so what about the contents of the whole file? It's okay, too. 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 line 5 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".

Now you will see that the contents of file1.txt are pasted after line 5 of file2.txt.

Line1line2line3line4line5ostechnixopen sourcetechnologylinuxunix

To save changes in the current file, type:

ZZ

To save all changes to all files and exit the vim editor, enter:

Wq method 2

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 w (that is, press CTRL + w and press w again). Alternatively, you can use the following shortcuts to move between windows:

CTRL-w k-window above

CTRL-w j-the window below

To open multiple files in a vertical window, run:

$vim-O file1.txt file2.txt file3.txt

To switch between windows, press CTRL-w w (that is, press CTRL + w and press w again). Alternatively, use the following shortcuts to move between windows:

CTRL-w l-window on the left

CTRL-w h-window on the right

Everything else is the same as described in method one.

For example, to list the currently loaded files, run:

: buffers

Switch between files:

: buffer 1

To open another file, type:

: e file3.txt

Copy the entire contents of the file to another file:

: r file1.txt

The difference between method 2 is that as long as you use ZZ to save changes to the current file, the file will be automatically closed. Then, you need to type: wq to close the file. However, if you follow method 1, when you enter: wq, all changes will be saved in all files and all files will be closed immediately.

For more details, see the man pages.

The above $man vim is all the contents of the article "how to use the Vim Editor to Edit multiple Files". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report