Because Vim itself is powerful and can use many commands, and can call external commands by executing colleagues, this makes it possible for us to customize tools and operations! Today, I will take Java compilation and execution as an example to briefly describe how Vim can customize shortcuts.
Commands
Vim defines the command map for the shortcut key. The corresponding unmap command cancels the defined shortcut key!
Step 2
How can we compile a file named hello. Java?
Run ESC in command mode and enter :! Javac hello. Java
: The command input prompt indicates that the following command is input by VIM.
! Indicates the operation after this. It is a command other than the editor.
Not to mention javac hello. java.
However, since we want to use this operation as a shortcut key call, the name of the file name must have its wildcard and cannot be too limited!
Here we use the wildcard %
Therefore, the entire command operation process is to press ESC and then input :! Javac % and press ENTER
Next, we can define the shortcut key. I define F12, so its command is
: Map <F12> :! Javac %
In this way, a problem is found... What's the problem... In the first place, if not in command mode... This can only be executed .... So we have to enter ESC first, but when we Press ESC in map, what should we do if the input state is exited? The answer is to replace the operation with the <ESC> character, then the command becomes
: Map <F12> <ESC> :! Javac %
Press enter to confirm and press F12 to display on the screen... But I still want him to execute it directly... Since <ESC> can replace the ESC operation, what can replace the carriage return operation? The answer is <CF> the complete compilation shortcut is completed:
: Map <F12> <ESC> :! Javac % <CF>
Next, define the execution command Ctrl + F12:
: Map <C-F12> <ESC> :! Java % <CF>
We should have already thought of this question. Java execution cannot carry extensions. What's more, the current extension is. java. What can we do to prevent it from having an extension? The answer is % <, % <, indicating a file name without an extension. Then the entire execution statement is:
: Map <C-F12> <ESC> :! Java % <CR>
In this way, the shortcut key can be used... Is it convenient?
Other special symbols are attached:
<Tab> Tab
<EOL> end of line
<BS> return