Learning VI and VIM editor (5): Crossing the base fence

Source: Internet
Author: User

This chapter summarizes the commands previously learned such as "C", "D", "Y", and learns some new knowledge: Other ways to enter VI, use buffers to store dragged or deleted text, and how to mark in a file.

A combination of more commands:

More than once, for "C", "D", "Y" commands have the use of "command" "Textobject", that is, these editing commands can be used in combination with the cursor Movement command, through the cursor Movement command to indicate the editing command will affect the text range. These combined commands are now briefly summed up and are a review of previous learning knowledge:

Change Delete Copy from the cursor position to ...

CH DH YH Top of current screen

CM DM yM current screen Middle

CL dL yL current screen bottom

c+     d+          y+ Next line

c5|    d5|          y5| The bank's fifth column

C2)   D2)       y2) second sentence down (end of document direction)

c{ d{ y{ forward a paragraph (file start direction)

C/pattern D/pattern Y/pattern where the pattern first appeared

cn    DN        yn Next Mode

CG    DG        YG to end of file

c9g  d9g  y9g to the Nineth line of the file

Option to open VI:

I have previously said "VI filename" This simple way to use the VI Editor to open the file, there are many useful VI command open options. Advance to a specific location: You can specify in the command line how the cursor will move the first time after the file is opened, such as by line number or search results.

VI +n file: Open files on nth line;

VI + File: Open file on the last line;

VI +/pattern file: Open file where the first pattern appears;

These commands can use "+", or "-", generally speaking, both methods can be accepted, but the actual test, the discovery of "-" error will appear, as shown in the book is wrong? Just record it for the time being.


The open options for these VI editors are very useful for programmers. For example, when an error occurs when compiling a source file, the compiler will display the relevant line number information for the compilation error. So we can navigate directly to the corresponding line when the file is opened, as shown in:


Moreover, when we are editing the file, if we have to edit halfway and have to leave temporarily, we can mark it in the current position with a certain pattern, such as "Here", when we return, we only need "VI +/here file".

Read-only mode: You can open a file in read-only mode so you don't have to worry about inadvertently modifying the file:

Vi-r filename;

View filename;

The view command is also like the VI command, in fact the view is only a link to VI:

If you want to modify the file and save the changes in read-only mode, you need to use the: w! "。 The exclamation point represents the overwrite of the read-only mode.

Recovery buffer: When editing a file, if an error occurs on the system, the file changes made since the last archive will disappear, and you can use the-r option to restore the edit buffer when the system freezes.

Vi-r or ex-r: Get a list of all files saved by the system;

Vi-r FileName: The edit buffer of the file name can be restored;

:p Re: Forces the system to save the buffer even when there is no dead machine;

good use of buffers:

As previously mentioned, for the last deleted or dragged content (the delete and drag operations share the same buffer) will be saved to the buffer, you can use the drop command to put the text back into the file.

At the same time, the VI editor saves the last nine deletions to a numbered buffer and can recover any of those buffers. However, only the deletion of this article in the behavior unit will be saved to the number buffer , for deleting a part of a row, or part of a word, etc., will not be saved to the number buffer, at this time only after the delete operation occurs immediately after the P command to recover.

In these number buffers, the last deleted content is saved in Buffer 1, the second-to-penultimate deletion is saved to buffer 2, and so on. If you want to recover a number buffer content, first enter "(double quotation marks), specify the buffer number, and then use the drop command, such as" 1p.

If you are not sure which buffer contains the content you want to recover, you do not need to keep typing "NP." Instead, because the repeat command is used after the P command. , the buffer's number is automatically incremented , combined with the Undo command (U), to progressively view the contents of each buffer by number, for proper recovery with the following command:"1pu.u.u.u. And so on

Also,VI can place dragged or deleted text in a letter-labeled named Buffer , which can be saved to 26 buffers (A-Z), and can be recovered at any time during an edit session using the drop command. If the text is already in a named buffer and the text continues to be placed in the buffer, the new text overwrites the text that already exists in the buffer, that is, the previous text disappears.

to save text to a named buffer, you need to precede the drag or delete command with double quotation marks (") and the name of the buffer (A-Z), for example:" Add the row content that is about to be deleted to a buffer named a. " when recovering, you only need to enter double quotes, plus the name of the buffer (A-Z), and the place command p to restore the contents of a named buffer, such as "AP." when you specify a buffer with uppercase letters, the dragged or deleted text is appended to the appropriate buffer.

To mark a place:

You can make an invisible bookmark somewhere in the text, and then, when you edit elsewhere, you can return to the corresponding bookmark location based on the relevant command. Note that the markup is valid only in the current edit session and is not stored in the file.

MX: marks the current position as x (x can be any character);

' x: (single quotation marks) moves the cursor to the first character of the line where the marker x is located;

' X: (anti-quote) moves the cursor to the character marked with X;

': (two anti-quotes) after moving the position, go back to the previous marker;

": (two single quotes) back to the beginning of the line where the previous marker was located;


This chapter VI editor commands summarize:

VI +n,vi +,vi +/pattern,vi-r,view, Vi-r,:p Re, "1p," Ayy, "ap,mx, ' X, ' X, '".


Learning VI and VIM editor (5): Crossing the base fence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.