Learning vi and vim editors (5): go beyond the barriers of basic knowledge

Source: Internet
Author: User

Learning vi and vim editors (5): go beyond the barriers of basic knowledge

This chapter summarizes the previously learned editing commands such as "c", "d", and "y", and learns some new knowledge: other methods for entering vi, use a buffer to store dragged or deleted text and how to mark objects.

More command combinations:

It has been mentioned more than once before. For commands such as "c", "d", and "y", they have the form of "command" "TextObject, these editing commands can be combined with the cursor movement command. The cursor movement command is used to indicate the text range that the editing command will affect. Now, a simple summary of these combined commands is also a review of previous learning knowledge:

Change Delete copy from cursor position ......

CH dH yH current screen top

CM dM yM middle of current screen

CL dL yL at the bottom of the current screen

C + d + y + next line

C5 | d5 | y5 | fifth column of the row

C2) d2) y2) down to the second sentence (end of the file)

C {d {y {forward paragraph (start direction of the file)

C/pattern d/pattern y/pattern down the first place where pattern appears

Cn dn yn next mode

CG dG yG to the end of the file

C9G d9G y9G to the ninth line of the file

Open vi options:

We have already talked about the simple method of "vi filename" to open a file using the vi editor. There are also many useful vi command opening options. Forward to a specific position: You can specify the method for moving the cursor for the first time after opening the file in the command line, for example, moving Based on the row number or search result.

Vi + n file: open the file in line n;

Vi + file: open file in the last line;

Vi +/pattern file: open file in the first place where pattern appears;

You can use "+" or "-" in these commands. Generally, both methods are acceptable. However, an error occurs when you use "-" in actual tests, as shown in, is there an error in the book? Record it for now.


Open Options of these vi editors are very useful for programmers. For example, when an error occurs when a source file is compiled, the compiler displays the row number information related to the compilation error. Therefore, you can locate the corresponding row when opening the file, as shown in:


In addition, when we edit a file, if we have to temporarily leave the file, we can mark it in a certain mode at the current position, such as "HERE ", when we get back, we only need "vi +/HERE file. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 1ru2wcSjyr2jur/examples/qtK7uPbOxLz + examples + dmlld8P8we7Sss/xdmnD/examples = "http://www.2cto.com/uploadfile/Collfiles/20141117/2014111709271748.png" alt = "\">

If you want to modify the file and save the modification in read-only mode, you need to use ": w! ". The exclamation point indicates that the read-only mode is overwritten.

Restore Buffer: if an error occurs during file editing, the file changes made since the last archive will disappear. You can use the-r option to restore the edit buffer when the system crashes.

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

Vi-r filename: restores the editing buffer corresponding to the file name;

: Pre: forces the system to save the buffer even if there is no dead end;

Shujia exploits the buffer zone:

As mentioned earlier, the last deleted or dragged content (the same buffer zone is shared by the delete and drag operations) will be saved to the buffer zone, you can use the placement command to put the text back to the file.

In addition, the vi editor saves the last nine delete operations to a numbered buffer to restore any of these buffers. However, this document will be saved to the serial number buffer only when it is deleted in the unit of action. For deleting a part of a row or a part of a word, it will not be saved to the serial number buffer, in this case, you can use the p command to restore the deletion immediately after the deletion operation occurs.

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

If you are not sure which buffer contains the content to be restored, you do not need to constantly input "np" to try. On the contrary, because repeated commands are used after the p command ". ", the buffer number will be automatically added, and then combined with the undo command (u), you can gradually view the content of each buffer by number, so that the correct recovery, the command form is as follows: "1pu. u. u. u. and so on.

At the same time, vi can put the dragged or deleted text in the naming buffer with letters as the label. These texts can be saved to 26 buffers (a-z, in addition, you can use the placement command to restore the text when editing the session. If text is already stored in a named buffer and the text is placed in the buffer, the new text will overwrite the existing text in the buffer, that is, the previous text will disappear.

To save the text to the name buffer, you must add double quotation marks (") and the buffer name (a-z) before dragging or deleting the command. For example: "add: the content of the row to be deleted is saved to the buffer named. During restoration, you only need to enter double quotation marks, add the buffer name (a-z), and place the command p to restore the content of a named buffer, such as "ap. When the buffer zone is specified with uppercase letters, the text dragged or deleted will be appended to the corresponding buffer zone.

Mark One location:

You can create an invisible bookmarks somewhere in the text, and then edit the bookmarks elsewhere, you can return to the corresponding bookmarks based on the relevant commands. Note that the tag is valid only in the current edit session and is not stored in the file.

Mx: Mark the current position as x (x can be any character );

"X: (single quotes) move the cursor to the first character marking the row where x is located;

'X: (reverse quotation marks) move the cursor to a character marked with x;

'': (Two backquotes) return to the previous mark after moving the position;

'': (Two single quotes) return to the beginning of the row where the previous mark is located;

Summary of the vi editor commands in this chapter:

Vi + n, vi +, vi +/pattern, vi-R, view, vi-r,: pre, "1 p," ayy, "ap, mx, 'x, 'X ,'',''.


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.