Editor Vim for programmers
Vim exists like a god in the editor. Mastering Vim greatly improves the efficiency of text editing. This article describes the most basic operations and skills of Vim.
Operation Mode
There are four modes in Vim:
- Normal Mode: After Vim is started, it is normal mode. All keys are function keys.
- Insert mode: Press key I to enter insert mode. You can enter text, return to normal mode, and press the Esc key.
- Visual mode: press the key v to enter the visual mode. You can move the cursor to select the text.
- Command Line Mode: Enter ":" To Enter command line mode and run the Vim command
Basic Movement
Move the cursor up, down, left, and right, corresponding to the four keys of kjhl. You can add a number to the front to move multiple units. For example, 10j moves 10 rows down.
Move in file
G ----- move to the last row
Gg ------ move to the first line
10G ------ jump to 10th rows
75% ----- jump to the full text 75%
Move to specified character
You can use f, t, F, and T to quickly move the current row.
Fx ----- move to the first 'X' character on the right of the cursor
Fx ----- reverse lookup, that is, moving to the specified character on the left of the cursor.
Tx ----- move before the first 'X' character to the right of the cursor
Tx ----- It moves after the specified character 'X' on the right of the cursor.
----- Repeat the previous input command f, t, F, T
, ----- Repeat the previous f, t, F, and T commands in the opposite direction.
";" And "," can also be expressed by a number before the two commands. You can also use a number before the command to indicate a multiple. For example, 3fx ----- move to the 3rd 'X' character on the right of the cursor.
Move to the end of the first line
0 ------ move to the beginning of a row
$ ------ Move to the end of the row
^ ------- Move to the beginning of the first non-blank line
Move by word
W ------ move the cursor to the beginning of the next word
B ------ move the first word of the cursor
E ------ move the end of the next word of the cursor
Ge ------ move the last word of the cursor.
Move by window
H ------ let the cursor jump to the top of the current window M ------ let the cursor jump to the center of the current window L ------ let the cursor jump to the bottom of the current window
Scroll screen relative to the cursor
Zt ------ move the cursor row to the top of the window zz ------ move the cursor row to the middle of the window zb ------ move the cursor row to the bottom of the window
Various Inserts
I ------ insert a at the cursor ------ insert o after the cursor ------ Insert a new row after the current Row O ------ Insert a new row before the current row cw ------ replace from the cursor position to a word the ending character (c and w)
Copy and paste
Yy ------ copy the current row p ------ paste it. You can also add a number before yy and p: 3yy ------ copy the three lines starting from the current row 3 p ------ paste the text three times
Search
Search in normal mode. /------ Enter "/" in Normal mode, enter the string you want to query, and press enter to jump to the First Matching place. ? ------ Search n in the opposite direction ------ repeat the last query command N ------ repeat the last query command in the opposite direction
Input "/" or "? "Then, use the, bottom mouse key (or CTRL-P/CTRL-N) to review the history record, and then execute this lookup again. You can also use "q/" and "q? "Command: open a new window at the bottom of the vim window. This window lists your search history. You can use any vim edit command to edit the content of this window, then press enter to search for the content of the row where the cursor is located. As shown in:
* ------ Search for words at the cursor position. All matching words are highlighted and jumped to the next matching item. # ------ search for words at the cursor position. All matching words are highlighted, and jump to the last matching item.
% ------ Matching brackets move, that is, to the position of the matching brackets with the current cursor
Replace string
% S/source string/destination string/g ------ replace all source strings in the current document with the destination string
Delete, undo, and redo
Dd ------ Delete the current row dt ------ Delete the current row until it is followed by the symbol u ------ undo the last operation ctrl + r ------ redo if I use dd to delete the current row before, if you regret it and do not want to delete it, you can use the u command to undo it. If you still want to delete it, you can use ctrl + r to continue the deletion.
Open, save, and exit
Unless otherwise specified, the following operations are performed in command mode, that is, ":" must be added before each command, for example, exit q. Q ------ exit the current file. q! ------ Force exit current file, do not save w ------ Write File, save disk wq ------ save exit current file x ------ save exit current file ZZ ------ save exit current file (run in Normal Mode) e filepath ------ open a file saveas filepath ------ Save As bn ------ after opening many files at the same time, you can use bn to switch to the next file. Bp ------ after opening many files at the same time, you can use bp to switch to the previous file.
Repeated commands
. ------ Repeat all the commands before the last exit of insert mode n <command> ------ repeated command commands mentioned earlier n times
Use cursor movement and command together
0y −−−−−− copy this line (0 moves to the beginning of the line, y starts copying from here until the last character of the line) ye ------ copy the word ending from the current position to the next word
Tip: Find the same line in the current document
:sort/^\(.\+\)$\n\1
Undo by Time
Vim 7 includes a new feature that allows users to jump to any editing point before or after. For example
:earlier 10m
You can return the editing Status 10 minutes ago. For example
:later 5s
You can jump to the editing point 5 seconds later. In addition, you can use the undolist command to view the list of Undo branches in the buffer zone. The undo <number> command can be used to move to a removed branch.
Delete all blank lines in the current document
:g/^$/d
Insert a specified string at the beginning or end of each row
Insert the first row:
:%s/^/your_word/
Insert at the end of the row:
:%s/$/your_word/
For example, insert a row number before each row as follows:
:%s/^/\=line(".")/Read shell commands
For example, insert date:
:r!date
: R is the abbreviation of read ,! Is to run a shell command, which means that I want to read the shell command output to vim.
Count the number of times a word appears
:%s/word//gn
The statistical result is displayed in the status bar at the bottom.
Vim paste indentation
After the Python code is pasted in Vim, the indentation is messy. After entering paste mode, you can paste the content in insert mode without any deformation.
:set paste
Use: set nopaste to restore the normal mode.
Well, this article has already mentioned a lot of basic Vim operations and skills. If you can master these skills, you can use Vim smoothly. There will be