Vim - The ubiquitous text editor.
Introduction
Vim is a text editor developed from vi. It is particularly rich in functions that facilitate programming, such as code completion, compilation, and error jumping, and is widely used among programmers.
Install
Linux systems usually come with Vim. Open the terminal and enter vim to enable it.
If you need to install it manually, Vim's official website provides a downloadable instruction document, just compile and install it according to your needs.
Vim modes and commonly used keys
The basic operations of Vim will be described in the tutorial that comes with Vim. Open the terminal and enter vimtutor to enter the tutorial.
These operations usually take twenty to thirty minutes to become familiar with.
Command Mode
The default mode after entering Vim.
In this state, typing on the keyboard will be recognized by Vim as a command instead of inputting characters. For example, if we press i at this time, a character will not be entered. i is regarded as a command.
Vim's arrow keys are ↑, ↓, ←, →, or h, j, k, l.
↑(k)
^
(h)← < > →(l)
v
↓(j)
The following are commonly used commands in command mode:
iSwitch to input mode and start inputting text at the current position of the cursor. Press the Esc key to return to normal mode.xis used to delete a character after the cursor.:Switch to bottom line command mode to enter commands on the bottom line.aSwitch to input mode and start inputting text after the cursor.oswitches to input mode and inserts a new line under the cursor;Oswitches to input mode and inserts a new line under the cursor.ppastes the clipboard content below the cursor;Ppastes the clipboard content above the cursor.dddeletes the entire line where the cursor is located.- The
dcommand also deletes, and is usually used in conjunction with other keys. uUndo the last change to the text.- The
ycommand can copy the selected area. You need to pressvto enter visual mode operation. yycopies the current line.Ctrl + rRedo the last undone operation.:wsaves the file, usually with q to save and exit.:qExit Vim.:q!Force quits Vim without saving changes.
Some other commands:
- The
ccommand is used to modify, equivalent todi. - The
=command can apply automatic indentation to the selected lines in the default format. ==automatically indents the current line.- The
.command can repeat the last executed command. - The
ggcommand can jump to the beginning of the code; theGcommand can jump to the beginning of the last line of the code; theGcommand can be preceded by a number to jump to the specified line. wcan jump to the beginning of the next word;ecan jump to the end of the current word or the next word;bcan jump to the beginning of the current word or the previous word;0can jump to the beginning of the line;$can jump to the end of the line.w,e, -0,$can also be combined with other commands, such asde,dw,d0andd$respectively corresponding to delete to the end of the word, delete to the beginning of the next word, delete to the beginning of the line and delete to the end of the line.
Press / in command mode, a search box will appear below, enter the characters you want to find, and press Enter to view the search results. If there are multiple search results, press n to jump to the next search result; press N to jump to the previous one.
In command mode, press * to search for the word under the current cursor.
If you enter a number n before entering a command, the command will be repeated n times.
Insert Mode
Press i in command mode to enter input mode, and press Esc to return to command mode.
In input mode, the following keys are available:
- Character key and Shift combination to enter characters
- ENTER, carriage return key, line feed
- BACK SPACE, backspace key, deletes the character before the cursor
- DEL, delete key, deletes one character after the cursor
- Arrow keys to move the cursor in text
- HOME/END, move the cursor to the beginning/end of the line
- Page Up/Page Down, page up/down
- Insert, switch the cursor to input/replace mode, the cursor will become a vertical line/underline
- ESC, exit input mode and switch to command mode
Press Ctrl+o in input mode to enter "Input - Command Mode". After performing an operation, it will automatically return to input mode.
Bottom line command line mode
Press : in command mode to enter bottom line command mode.
The bottom line command mode allows you to enter single or multiple character commands, and there are many available commands.
In the bottom line command mode, the basic commands are:
:help/:hView the English version of Vim online help documentation.:wSave the file.:qExit Vim.:wqSave the file and exit Vim.:q!/:!qforce quit Vim without saving changes.:e filenamecan open the specified file in the current directory.- The
:scommand is a replacement.
" Replace the first matching str1 on the current line with str2
:s/str1/str2/
" Replace all str1 on the current line with str2
:s/str1/str2/g
" Replace all str1 on the current line with str2, asking before replacement
:s/str1/str2/gc
" Replace the first matching str1 on each line from x1 to x2 with str2
:x1,x2 s/str1/str2/
" Replace all str1 on each line from x1 to x2 with str2
:x1,x2 s/str1/str2/g
" Replace all str1 on each line from x1 to x2 with str2, asking before replacement
:x1,x2 s/str1/str2/gc
" Replace the first matching str1 on every line with str2
:%s/str1/str2/
" Replace all str1 in the entire file with str2
:%s/str1/str2/g
" Replace all str1 in the entire file with str2, asking before replacement
:%s/str1/str2/gc
If the command format is :! command, the command will be executed in the bash terminal.
Press the Esc key to exit the bottom line command mode.
Visual mode
Press v to enter visual mode, mostly used to select areas. Press V (Shift+v) to enter row visual mode, which is used to select rows.
Press Ctrl+v or Ctrl+q to enter visual block mode (visual block).
After entering the block visual mode, press I or A to enter the insert mode (equivalent to i and a). After exiting the insert mode, the changes made to this row will be applied to the same position of each selected row. Often used to add comments in batches.
After selecting, enter y or d to execute the corresponding command.
The three visual modes can be converted into each other by pressing buttons.
Vim shortcut keys
You can refer to The most complete Vim shortcut key bitmap in history—from entry to advanced
Advanced knowledge
. command
Vim users will inevitably resist repeated text modifications, because Vim is destined to have two more keystrokes than other editors - Esc and i. However, Vim actually provides the repeat command ., which is suitable for repeated adding, modifying, and deleting text operations.
The . command can repeat the last executed command. But this "command" is not limited to a single command, it can also be a combination of 数字 + 命令; 进入插入模式 + 输入文本 + Esc is also a type of command. Therefore, the highest efficiency can be achieved by using the . command appropriately.
For example, the following code is missing a semicolon at the end of each line:
int a, b
cin >> a >> b
cout << a + b
return 0
By moving . to the end of the line and inserting the command A, you can efficiently add the semicolon at the end.
A;<Esc>
" Repeat the command below
j.
For another example, in the following code, the array names of the following five assignment statements are all written incorrectly:
int check() {
book[1] = 1, book[2] = 1, book[3] = 1, bok[1] = 1, bok[2] = 1, bok[3] = 1,
bok[4] = 1, bok[5] = 1;
return 0;
}
It is too troublesome to change them one by one, and the s command in command line mode will change them all.
The first modification method is to use it with the s command in normal mode (delete the character at the cursor and enter insert mode). Come to the first letter of the wrong array name, press 3s/cw, enter the correct array name and exit. Then move the cursor one by one and use the . command.
The second more time-saving modification method is to use search mode modification. Type /bok, then press Enter, use the n key to go to the first letter of the wrong array name, type 3s 新数组名 <Esc>, and finally repeat n..
The third modification is the simple search command f. In normal mode in a line, press f + 单个字符 to find the character appearing in the line and move the cursor to the character; press ; to find the next one, and , to find the previous one. So for the above code, just type fb;;; and then enter insert mode to modify it, then ;.. This modification is suitable for situations where only intraline movement is required.
Macro
Vim's macro function can repeat commands of any length.
Before using macros, you must first "record", that is, record a series of key operations and then play them back, thus achieving a repetitive effect. The recording method is very simple. In normal mode, type q to start recording. Next, specify an execution command key for the recorded macro. You can press any of the 26 letters to specify. At this time, 记录中 @刚刚选择的字母 will be displayed in the lower left corner. Then you can start recording commands. In the same way, press q in normal mode to pause recording.
The method of use is to press : to enter the command line mode, type @选择的记录字母, and then the previously recorded command will be called.
Combining . with a macro, that is, recording a macro → calling the macro → . and repeating the command → number + ., can achieve very high efficiency.
normal command
This command is related to normal mode, and its effect is to repeat the command on the specified line.
Press : to enter the command line mode and enter the following command:
:a,b normal command
or:
:a,b norm command
The above command means to execute the command command on the a~b line in normal mode.
Since the normal command can be called repeatedly by the . command and is easy to understand, its usage frequency is even higher than that of macros.
number +.+ macro + normal
The above three commands can be used in combination. For example:
I downloaded a book, and I need each chapter to be turned into a "title" so that it can be easily converted into a format such as mobi, or to easily generate a TOC directory jump. What should I do?
The following is the process using Vim:
- Press/call up the search box and enter a regular expression to search;
- Use the
qcommand to start recording macros; - Type the
I#command and press ESC; - Use the
qcommand to end macro recording; - Type
normal n@字母to go to the next place and repeat the previous step; - Type
数字 + .multiple times.