[Glossary]
In this article, the character sequences that can cause actions in vim are collectively referred to as "commands", which not only include commands starting with: In command line mode, but also key sequences in other modes.
Note]
As vim is closely related to each other, other theme related to it is not fully considered in the description in this article, which may lead to inaccurate description.
1 vim works in a certain mode all the time
Vim adopts the design philosophy of "different modes". It has many modes, common ones are normal (general mode), insert (insert mode), visual (visual mode ), command (command line mode) and replace (replace mode ).
Different models are isolated from each other, which is also the biggest advantage of this design method. In this way, you can use the same command name in different modes without conflict.
2. All input commands in vim are different from those in other editors. All the key sequences in vim may be commands, even in insert mode. For example, in the insert mode, if you enter the character on the keyboard, vim first identifies whether the character you entered is a defined command. If not, it is inserted into the buffer as is.
The basic process for user input in vim is as follows: (1) user input (2) vim identifies whether the user input is a command name; (3) if the user input is a command name; (4) (3) execute this command and go to (5) (4) default action (for, the insert mode inserts the input into the buffer, and for other modes, no operation is performed) (5) complete input 3 vim built-in commands and user-defined commands 3.1 vim has built-in commands in each mode, and insert mode is an exception in different modes, vim has built-in essential commands. For example, in normal mode, the familiar commands j, k, h, and l are used to move the cursor, delete a line of command dd, and paste the command p. In command line mode, the command for writing files is w, exit command: q, SEARCH Command/something, etc. Some built-in commands can work in multiple modes. For example, the command yy can work in normal mode, it can also work in visual mode. Specifically, in the insert mode, vim has almost no built-in commands. Maybe the original insert mode is the most important function in the insert mode. 3.2 user-defined commands are the powerful built-in commands of vim. vim supports user-defined commands from the very beginning, which is why vim can become vim. For different modes, vim provides two custom modes: for the command line mode, use: command to define; for other modes, use: map to define. In this article, we only describe the map method. 3.2.1 a small example of custom commands in insert mode is provided. As mentioned above, there seems to be no built-in commands in vim insert mode. Therefore, we should start with it and customize the commands in insert mode. Input the following in vim: imap tks thanks [note]. I indicates the insert mode, map ID ing, and tks indicates the custom USER command name, thanks is the name or Character Sequence mapped.
Then, enter tks in insert mode to check the effect. We can see that: (1) After t is input, the cursor is still below t, but not behind t; (2) After k is input, k appears at the t position, t is overwritten, And the cursor remains unchanged; (3) when the input s continues, thanks appears at the input position, and the cursor moves to the back. 3.2.2 analyze the execution process as follows: (1) After t is input, vim recognizes that t is the beginning of the User-Defined command tks, however, it cannot be determined that the user entered the tks command, so the user will continue to wait for the subsequent input for further judgment; (2) when the k is entered, vim recognizes that the tk entered by the user is part of the tks command, but it still cannot determine the user's intention at this time, so it continues to wait for subsequent input; (3) when s is entered, vim recognizes that the tks entered by the user is the tks command. Therefore, when executing this command, the operation of this command is to input the sequence of thanks; (4) vim continues to identify whether thanks is the name of a command. Because there is no command named thanks, it inserts thanks into the buffer as it is, refresh the output, and displays the characters thanks. 3.2.3 The Infinite Loop problem shows that the map-completed command ing function maps one command to another command or character sequence. In addition, map will recursively execute, that is, if the ing result is a command, continue to execute this result command. Imap a B: imap B c and then input a in insert mode. The output result is not B, but c. This method will bring about the classic endless loop problem, as shown below: imap a B: imap B c: imap c a. Fortunately, vim itself can identify this endless loop and will prompt E223: recursive mapping. However, in practice, you must avoid this endless loop. Vim also provides us with a way to avoid it, that is, when the noremap command is as follows: inoremap a B: inoremap B c: inoremap c a uses: noremap for ing, the ing result is directly output as a string, not as a command. Therefore, the result of the above ing is that input a gets B, input B gets c, and input c gets. 3.2.4 The leftmost longest command name principle defines the following two commands.: Imap a oneA: imap aa twoA so when we input aa, vim will interpret aa as an aa command, or two a commands? Experiments show that vim adopts the length-first principle. After a user is input, vim cannot determine the user's intention and can only wait. When the character a is another input, at this time, vim can determine that the user entered the aa command (because there are no other commands starting with aa), so run the aa command and output the twoA, if the second character entered by the user is not a, vim can also determine that the user entered the command, so the oneA will be output and the second character entered by the user. In actual work, try not to make different commands have the same starting part to increase the speed of vim response.
4. Other issues that need to be paid attention to during the ing operation the previously discussed ing execution process, the endless loop problem, and the command name longest principle are applicable to custom commands in all modes. Next we will discuss several other issues that need attention. 4.1 Special keys to map, often need to use special keys, such as function key F1-F12, Ctrl, Alt, Enter and so on. Vim provides a convenient expression for users. For example, <F1> indicates the F1 key, <CR> indicates the Enter key, and <Esc> indicates the Esc key. For a detailed list, see the vim Help online manual. For example, in normal mode, pressing the F2 key will input hello in the next row of the current row and return normal mode. : Nmap <F2> ohello <Esc> [explanation] o creates a new row and enters the insert mode. hello, insert directly. <Esc> the system returns the normal mode.
4.2 The number of command executions is also applicable to custom commands. For example, in normal mode, if you enter 10 and press the F2 key, 10 rows of hello will be entered.
4.3 try not to define commands in insert mode. Although the previous example uses imap to define custom commands in insert mode, this is a bad practice, because the insert mode should have completed the input as is, otherwise it will make the user feel uncomfortable. With the same function, you can use custom commands in normal mode.
4.4 try not to overwrite the built-in command vim by using the "post-Come" method. The "post-defined" command with the same name can overwrite the command with the same name defined first, and even the built-in command can be overwritten. For example, after nmap j <Nop>, the j used for navigation will become invalid. Although vim provides users with unlimited customization rights, maintaining the consistency of basic built-in commands is the basis for communication between vim users. Therefore, do not overwrite the built-in commands! Otherwise, vim will be totally invisible! 4.4 Other ing operations (1) view existing custom commands and use map commands without parameters. For example, view existing custom commands in Normal Mode: nmap (2) delete custom commands and use unmap commands. For example, after j: nunmap j is deleted, j can complete the built-in navigation function. Mapclear class commands delete all custom commands, such as deleting all the custom commands in insert mode: imapclear
4.5 The scope of other idle commands is not only affected by the mode, but also by the buffer. commands defined in one buffer zone do not work in the other buffer zone. User-Defined commands can also call built-in functions and user-defined functions, which will be analyzed when "commands in user-defined command line mode" is used. Custom commands can not only be performed in interactive mode, but can also be written to the vim configuration file or plug-in file (maybe this is a more useful method ). Writing vim plug-ins is a big topic. This article is just a simple Getting Started Guide For vim custom commands. If it can benefit readers a little, thank you.