VI operation multi-file N solution solution

xiaoxiao2021-03-06  19

VI operation multi-file N solution solution

By Guipei

2005-3-21

Learning Linux, there is always less use of learning the VI editor. The role of VI If compared with the window system, I think the functionality should be the same as Notepad. But as a particularity of Linux, the complexity of VI is much higher than the operation of Notepad.

Recently, I have learned some of the operation methods of multiple VI operations. I summarize here, because I have seen a lot of books, or the information, most of them introduce the basic functions of VI, and there is no in-depth, this article is basically referred to. This book: Linux in a nutshell and O'Reilly - Learning The Vi Editor is also a summary of the VI operation multi-file.

First mention a question, why do you want multi-file operation? Many Linux profiles are saved in the / etc directory or software in the property file, so that we will copy a backup before modifying the configuration. If you find problems, we can restore old configuration, or based on old Configuration, modify a new configuration. In this way, many cases we need to operate multiple files. Of course, there are other situations.

Previous old way:

1. Use the X system to open multiple console.

2, use remote operations, such as SSH, or Telnet multiple logins, open multiple console.

Although these situations can be easily solved, but in some special cases, such as the single user mode, these methods seem to be powerful, even in the multi-user mode, open multiple console, can not be in multiple VIs Copy, copy, etc. I have to open the old file, take a pen, record the file content, then turn off the file, open the file you need to modify, edit it.

This is really a painful experience. If you don't want to repeat my mistakes, see if something can use a single VI to operate multiple files, and operate, copy, paste, etc.

Ok, let's prepare two files:

File Fstab: ----------------------------------------------- -------------------------------------------------- ----------------- label = / / ext3 defaults 1 1none / dev / pts devpts gid = 5, Mode = 620 0 0none / dev / shm TMPFS Defaults 0 0none / Proc Proc Defaults 0 0none / Sys sysfs defaults 0 0Label = swap-hda4 swap swap defaults 0 0 0 / dev / fd0 / media / floppy auto pamconsole, exec, noauto, management 0 0 File Fstab.bak: --------- -------------------------------------------------- -------------------------------------------------- ----- / dev / hdd / media / cdrom auto pamconsole, ro, exec, noauto, managed 0 file fSTAB is the standard partition load configuration file for the Linux system. Here we have to add fstab.bak files to Document FSTAB, let us use several ways to handle:

Method 1: Multi-file opening method:

We can use commands: vi fstab fstab.bak, VI itself supports opening multiple files.

Use the vi command: buffers lists the currently open files, and numbers,

Use: Buffer i to switch the current file, you can use YY and PP to copy the file.

Operation order:

1, enter the command, we can see the output :: buffers 1% a "fstab" line 12 2 # = "fstab.bak" line 12, switch to the second vision, copy it :: buffer 2 "fstab.bak" [readonly]

2L,

95C3, copy the first line of content in the file: yy4, switch back to the first file:: buffer 2 "fstab"

12L,

808C5, paste copy content, entry Y copy command in the insertion point: P

We can see that the operation has been completed.

Method 2: Multi-window Method:

Vi itself also supports multi-window operations, you know, see how we do:

Operation order:

1. Open the first file fSTAB: VI FSTAB2, open the second file in VI :: new fstab.bak Wid you see the VI to cut the window to two editing windows? 3, copy the contents in the fstab.bak, the current active window is fSTAB.BAK, and we enter the command yy to copy the current line. YY4, switch to the FSTAB window, this need to be used in accordance with the command: Ctrl w J command method is to press the CTRL control button and the W key, let the J key, have to see the cursor to move to the window below. 5, paste the selected content: POK, how, file copy is completed?

Method 3: Insert method:

Using Word, most of us have used a method of inserting a file, VI is also very COOL, which is also the same.

Operation order:

1. Open the first file fSTAB: VI FSTAB2, place the cursor to the insert point, enter the command :: R fSTAB.BAK3, you can already see that the content in the fstab.bak has been inserted into FSTAB.

Vi's function does not make it simple, about multi-file operation, more detailed operations can refer to the two books mentioned above. Master some VI's operations will make you more convenient to manage Linux.

转载请注明原文地址:https://www.9cbs.com/read-44615.html

New Post(0)