Learning VI and VIM editor (11): Advanced editing Methods (2)

Source: Internet
Author: User
Tags control characters delete key

This chapter learns how to save commands in the VI editor.

Save command:

There are many ways to save a long sequence of commands in VI and EX, so that when you call a saved sequence, you only need to enter a few characters.

Word abbreviations:

You can define abbreviations so that VI automatically expands the input into the original text when you are in insert mode. The command that defines the abbreviation is: "AB abbr phrase", where abbr is the abbreviation given to phrase. Note that in insert mode, the abbreviation is expanded only if the abbreviation is entered as a single word, and the abbr in the word is not expanded. as shown, define "VI" as the abbreviation for "visual editor".


by ": Unab abbr" To cancel the definition of an abbreviation, so that when you enter the abbreviation, the abbreviation will not be expanded. lists all currently defined abbreviations through the ": AB" command.

It is also worth noting that, in the actual test, if the abbreviation appears in the phrase represented, the abbreviation will only be expanded once, and will not be infinitely recursively expanded (but some versions of the VI editor may be infinitely expanded or the abbreviation definition fails).

using the map command:
The map command "corresponds" the command sequence to an unused key. So the map command acts like the AB command, but the map command defines a macro for the VI command pattern, whereas the AB command defines the macro for the insert pattern.

: Map x Sequence: Defines the character x map to the sequence command sequence;

: Unmap x Sequence: Cancels the command mapped by the character X;

: Map: Lists all mapped characters;

Note that it is best to map only keys that are not used in the command pattern. If the keys used in VI are mapped, the original function of the key disappears. And in the command mode is not used in the keys are (different versions of VI will vary): The letter key: G,k,q; Control keys: Ctrl+a,ctrl + K,ctrl + O,ctrl + W,ctrl + X; symbols: _,*,\ and =.

As shown, map the sequence of commands that exchange two words to "Q" ("DW" to delete a word, "e" to the end of the next word, "L" to move to the right, "P" to place the word just deleted), so that at any time in the editing session, you can use the Q key to exchange the order of two words:

Protect Keys from ex explanation:

When you define a map command, some keys do not enter directly and use them as part of the command sequence, such as the Enter,esc,backspace,delete key. If you need to use these keys as part of the command sequence, you need to precede the CTRL + V to transform the normal meaning of the keys. and after you enter CTRL + V, the "^" character is displayed on the screen, and after entering the ENTER key, the screen displays "^m". Similarly, the BACKSPACE key is escaped and appears as "^h", and the ESC escape is displayed as "^[".

This usage can be used in any ex command, not just the map command. This means that we can enter line breaks, backspace, etc. in the abbreviation command or the Replace command. For example, the NUM definition is abbreviated as follows:

When NUM is entered in insert mode, num is automatically expanded to:

There are a few things to keep in mind:

(1) the vertical bar (|) has a special meaning in ex (as a separator for multiple ex commands), so you cannot use a vertical bar in the mapping key of the insert mode.

(2) If you need to use the control character as a mapping character, for example, "Ctrl + a" to map to the sequence command sequence, the following command can be used ": Map CTRL + a sequence", but there are three control characters must be escaped with "Ctrl + V", respectively, " Ctrl+t "," Ctrl+w "," ctrl+x ". So if you want to map "ctrl+t", you need to enter ": Map Ctrl + V ctrl+t sequence" as follows. However, the actual test is not so, for example, you can directly map the "Ctrl+x", the front does not need to be escaped. Instead of "Ctrl + a" to be escaped, the book is wrong again, here first write it down.

(3) The mapping sequence can contain other mapping commands, which means that nested mapping sequences are allowed. This feature is controlled by the VI Editor's remap option, and the option is turned on by default.

Examples of mapping keys:

(1) Most of the time we press the "E" key to move the cursor to the end of the word to add content after the word, so you can do the following mapping:


This allows you to enter the "E" key in command mode and move directly to the end of the word and into insert mode.

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

Again, the button for "^m" displayed on the screen is: enter "Ctrl + V" First, and then enter "enter".

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

This allows you to annotate the line code by entering G directly in the command mode. In fact, there are a lot of methods, and then give a search command to implement the method:

mapping keys in insert mode:

In general, mapping should be useful only in command mode, because entering a key in insert mode should represent the input of the corresponding text instead of executing the corresponding sequence of commands. However, by adding an exclamation point (!) after the map command, you can force overwriting the original meaning of the key to produce the mapping behavior in insert mode.
For example, before facing the "E" key to map the following: "Map e ea", so that in the command mode to enter "E", you can directly move the cursor to the end of the word and into the insertion mode. However, if you use the "E" key in insert mode, the letter "E" is entered. If you want to achieve the same effect in insert mode, you need to define it as follows:


This allows you to enter the "E" key in insert mode to move the cursor to the end of the current word and still remain in insert mode. However, there is a problem: You can no longer enter the text "E". If you want the character "E" to return to normal text input mode, you can use the following command: ": unmap! E "(But this command does not allow the" E "key to revert to the input text when it is actually tested, is there a mistake in the book?) )。

The VI and Vim editor also describes the relevant knowledge of the mapping function keys, which can be consulted in the VI and Vim editor (p125).

This chapter VI command summarizes:

": AB", ": Unab", ": Map", ": Unmap", Escape key "Ctrl + V", ": map!", ": unmap!".


Learning VI and VIM editor (11): Advanced editing Methods (2)

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.