Vi and vim Editor (11): advanced editing method (2)

Source: Internet
Author: User
Tags control characters

Vi and vim Editor (11): advanced editing method (2)

Save command:

Vi and ex provide many methods to save a long string of command sequences. In this way, you only need to enter a few characters to call the saved sequence.

Abbreviation:

You can define abbreviations to enable vi to automatically expand the input to the original text when it is in the insert mode. The command for defining the abbreviation is "AB abbr phrase", where abbr is the abbreviation specified for phrase. Note: In the insert mode, the abbreviation is expanded only when the abbreviation is used as a single word input, and the abbr in the word is not expanded. As shown in, "vi" is defined as the abbreviation of "visual editor.

You can use ": unab abbr" to cancel the definition of an abbreviation. If you enter this abbreviation again, the abbreviation will not be expanded. Use the ": AB" command to list all definitions.

It is worth noting that, in actual testing, we found that if the abbreviation appears in the represented phrase, it will only be expanded once, it does not recursively expand infinitely (but some versions of the vi Editor may expand infinitely or the definition of abbreviations fails ).

Use the map command:
The map command "maps" the command sequence to an unused key. Therefore, the map command is similar to the AB command, but the map command defines the macro for the vi command mode, while the AB command defines the macro for the insert mode.

: Map x sequence: defines the string x mapped to the sequence command sequence;

: Unmap x sequence: cancels the command mapped to character x;

: Map: list all ing characters;

Note that it is best to map only keys not used in command mode. If you map keys that have been used in vi, the original functions of the key will disappear. Keys that are not used in command mode (different versions of vi may be different): Keys: g, K, q; control keys: CTRL + A, CTRL + K, CTRL + O, CTRL + W, CTRL + X; Symbol: _, *, \ and =.

As shown in, the command sequence for exchanging two word sequences is mapped to "q" ("dw" is used to delete a word, and "e" is moved to the end of the next word, "l" move one cell to the right, "p" place the words that have just been deleted), so that you can use the q key to exchange the order of the two words when editing the session:

Protection buttons are not explained by ex:

When defining a ing command, some keys cannot be directly entered and used as part of the command sequence, such as ENTER, ESC, BACKSPACE, and DELETE. If you want to use these keys as part of the command sequence, you need to add CTRL + V to the front to convert the normal meaning of the keys. After Ctrl + V is entered, the character "^" is displayed on the screen. After entering enter, the character "^ M" is displayed on the screen ". Similarly, the escape key is displayed as "^ H", and the escape key is displayed as "^ [".

This method can be used in any ex command, not just the map command. This means that we can enter line breaks, backspace characters, and so on in the abbreviated command or replacement command. For example, the abbreviation of num is as follows:

After inputting num in insert mode, num is automatically expanded:

There are several precautions:

(1) the vertical bars (|) have special meanings in ex (as the separator number of multiple ex commands). Therefore, you cannot use the vertical bars in the insert mode ing key.

(2) If You Need To map control characters as ing characters, for example, map "CTRL + A" to the sequence command sequence, you can use the following command ": map CTRL + A sequence ", however, three control characters must be escaped using "CTRL + V", namely "CTRL + T", "CTRL + W", and "CTRL + X ". Therefore, to map "CTRL + T", enter "map CTRL + v ctrl + T sequence" as follows ". However, this is not the case in actual tests. For example, you can directly map "CTRL + X" without escaping. On the contrary, "CTRL + A" should be escaped. Is it wrong in the book? Write it down first.

(3) The ing sequence can contain other ing commands, that is, the nested ing sequence is allowed. This function is controlled by the remap option of the vi editor and is enabled by default.

Example of ing key:

(1) Most of the time, we press the "e" key to move the cursor to the end of the word to add content to the end of the word. Therefore, we can perform the following ing:

In this way, you can enter the "e" key in command mode to directly move to the end of the word and enter the insert mode.

(2) Save a file and edit the next file:

Again, the button corresponding to "^ M" displayed on the screen is: ENTER "CTRL + V" first, and then ENTER "ENTER ".

(3) Comment out a line of code in C/C ++:

In this way, you can directly enter g in command mode to comment out the code. There are still many methods, and a method to implement the SEARCH Command is provided:

Ing key in insert mode:

In general, ing should only be useful in command mode, because inputting a button in insert mode should represent inputting the corresponding text, rather than representing executing the corresponding command sequence. However, you can add an exclamation point (!) after the map command (!), You can forcibly overwrite the original meaning of the button to generate the ing behavior in the insert mode.
For example, the following ing is performed for the "e" key: "map e ea". In this way, enter "e" in command mode ", you can directly move the cursor to the end of the word and enter the insert mode. However, if the "e" key is used in the insert mode, the letter "e" is entered ". To achieve the same effect in the insert mode, you need to define it as follows:

In insert mode, you can enter the "e" key to move the cursor to the end of the current word and remain in insert mode. But there is a problem: the text "e" is no longer input. To restore the character "e" to the normal text input mode, run the following command: ": unmap! E "(but in actual testing, this command cannot restore the" e "Key to the input text. Is there an error in the book ?).

The VI and VIM editor also introduces the knowledge of ing function keys. For details, refer to VI and VIM editor p125 http://www.bkjia.com/ OS /201511/342830.html.

Summary of the vi commands in this chapter:

": AB", ": unab", ": map", ": unmap", escape button "CTRL + V", ": map! ",": Unmap! ".

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.