Vim is an ultra-bull editor with powerful command functions. And most of these commands can be combined, for example, the 9yy command means to copy 9 lines of content, 9 means the number of rows to be copied, the same 100dd means to delete 100 rows, when the number and command cooperation, more powerful than the simple command, similarly, the C command means erase, w means word is the word, Then CW means erase a word, c5w means delete 5 words and so on. By crafting these simple commands together, you can play a powerful, unimaginable function.
Macro command (Macros)
Advanced some of the editor, will contain the macro function, Vim is certainly not missing, the use of macros in Vim is very convenient:
: QX |
Start recording macros and save results to register X |
Q |
Exit record mode |
@x |
Play macro commands recorded in the X register |
A little explanation, when entered in normal mode: After QX, all your edits to the text will be recorded, enter Q again to exit the recording mode, and then enter @x to repeat the command just recorded, this command can be followed by numbers, indicating how many times to repeat, such as @x20, Can be repeated 20 times. This is very useful in batch processing of text.
Edit multiple files at the same time
In the many Vim plug-ins, there is a plug-in called Minibuffer, that is, the following tab function, you can support the simultaneous editing of multiple files.
Label command
: Tabe fn |
Edit a file in a new tab FN |
Gt |
Switch to the next tab page |
GT |
Switch to previous tab page |
: Tabr |
Switch to the first tab page |
: tabl |
Switch to the Last tab page |
: TABM [N] |
Move the current tab to the Nth tab |
Yes, as you can imagine, with Eclipse, UE and other tabs is a meaning!
Window commands
Ctrl+w s |
Horizontal split Window |
Ctrl+w W |
Toggle Window |
CTRL+W Q |
Exits the current window (this command does not affect other windows because there are multiple files at the same time) |
Ctrl+w V |
Vertical split Window |
Other
Vim does not make any actual changes to the file until it is saved, but it is loaded into the buffer, and the edit of the file is actually the edit of the buffer until: W will be stored in the physical file.
: E File |
Load the file into a new buffer |
: Bn |
Jump to the next buffer |
: BD |
Delete buffer (close file) |
: SP FN |
Split the window and load FN into a new window |
Exit editor
: W |
Writes a buffer to a file, which is a save modification |
: Wq |
Save changes and exit |
: X |
Save changes and exit |
: Q |
Exit, if the buffer has been modified, you will be prompted |
: q! |
Force quit, discard modifications |
Find replacements
/pattern |
Search backwards for string pattern |
? pattern |
Search forward string pattern |
N |
Next match (if yes/search, then down next,? Search is up next) |
N |
Previous match (IBID.) |
:%s/old/new/g |
Search entire file, replace all old with new |
:%S/OLD/NEW/GC |
Search the entire file, replace all old with new, and make sure you replace it every time |
Copy and paste
Dd |
Delete the cursor in the row |
Dw |
Delete a word (word) |
X |
Delete current character |
X |
Delete the previous character |
D |
Delete to end of line |
Yy |
Copy a row, this command can be followed by a number, identity copy multiple lines, such as 6yy, to start copying 6 rows from the current line |
yw |
Copy a Word |
y$ |
Copy to end of line |
P |
Pastes the contents of the pasteboard below the current line |
P |
Pastes the Clipboard contents to the top of the current line |
]p |
With indented paste, Vim automatically adjusts the indentation of the Code |
"A |
Add content to/deposit a register to support multiple Pasteboard |
Attached: For example, a commonly used register is the system register, the name is +, so from the system pasteboard paste into Vim command for "+p, note here the + does not represent the operator, 21 registers."
Move cursor
Moving the cursor in VIM is very different from other editors, but once learned, it moves quickly in the text.
H,j,k,l |
Up, down, left, right. |
Ctrl-f |
Go up one page |
Ctrl-b |
Turn down one page |
% |
Jumps to the parentheses that match the current bracket, as currently in {, jumps to the matching} |
W |
Jump to the next word, split by punctuation or word |
W |
Jump to the next word of the head, long jump, as End-of-line is thought to be a word |
E |
Jump to the next end of a word |
E |
Jump to the end of the next word, long jump |
B |
Jump to the previous word |
B |
Jump to the last word, long jump |
0 |
Jumps to the beginning of a line, whether or not indented or jumping to the No. 0 character |
^ |
Jumps to the first character in the beginning of a line |
$ |
Jump to end of line |
Gg |
Jumps to the first line of the file |
Gd |
Jumps to the declaration of the variable at which the cursor is currently located |
N G |
Jump to the nth row, such as 0G, is equivalent to gg,100g is the 100th row |
Fx |
Find the X character in the current line and jump to |
; |
Repeat the previous F command without repeating the input FX |
Tx |
Similar to FX, but only jumps to the previous character of X |
Fx |
In the opposite direction of FX. |
),( |
Jump to top/Next statement |
* |
Find the word where the cursor is located, and look down in this combination of n keys, you can switch between each look-up content |
# |
Find the word where the cursor is located, and look up |
`. |
Jump to last edit location |
"Ctrl +}" can jump to the definition of a function or variable, "CTRL + O" can return to the previous jump page.
Move on the screen
H |
Move the cursor to the topmost line on the current screen |
M |
Move the cursor to the middle line of the current screen |
L |
Move the cursor to the bottom line on the current screen |
Bookmark
Ma |
Save the current position as a label |
' A |
Jump to label A |
Edit
R |
Replace one character |
J |
Connect the next line and the current row as one line |
Cc |
Delete the current row and enter edit mode |
cw |
Delete the current word and enter edit mode |
C $ |
Erases content from the current position to the end of the line and enters edit mode |
S |
Delete the current character and enter edit mode |
S |
Delete the line of the cursor and enter edit mode |
Xp |
Swaps the current character and the next character |
U |
Revoke |
Ctrl+r |
Redo |
. |
Repeat last Edit command |
~ |
Toggle case, current character |
G~iw |
Toggle the case of the current word |
Guiw |
Turn the current word into uppercase |
Guiw |
Turn the current word into lowercase |
>> |
Move the current line one unit to the right |
<< |
Moves the current line one unit to the left (one tab character) |
== |
Automatically indent as you go forward |
Insert mode
I |
Enter insert mode from the current cursor |
I |
Enter insert mode and place the cursor at the beginning of the line |
A |
Append mode, placing the cursor after the current cursor |
A |
Append mode, place cursor at end of line |
O |
Adds a new row below the current line and enters insert mode |
O |
Adds a new row above the current line and enters insert mode |
Esc |
Exit Insert Mode |
Visual mode
Tag text
V |
Enter visual mode, single character mode |
V |
Enter visual mode, line mode |
CTRL + V |
Enter visual mode, column mode, similar to UE's column mode |
O |
Jump cursor to another endpoint of the selected block |
U |
Turn the contents of the selected block into uppercase |
O |
Jump cursor to another endpoint of the block |
Aw |
Select a word |
Ab |
Check everything in parentheses, including the parentheses themselves |
Ab |
Check all contents in {} brackets |
Ib |
Check the contents of parentheses without parentheses |
Ib |
Check the contents of {} without {} |
To act on a marker
> |
Block Right Shift |
< |
Move the block to the left |
Y |
Copy Block |
D |
Delete Block |
~ |
Toggle the case of content in a block |
Keyboard diagram:
Summary of common replacement patterns in vim
1, simple substitution expression
The Replace command can replace another word with one word in the full text:
:%s/four/4/g
The "%" range prefix indicates that substitutions are performed on all rows. The last "G" tag represents all the matching points in the replacement row. If you are only working on the current line, just remove the%
If you have a word like "thirtyfour", the above command will go wrong. In this case, the word will be replaced with "Thirty4″." To solve this problem, use "\<" to specify the beginning of the matching word:
:%s/\<four/4/g
Obviously, this will still make a mistake when dealing with "fourty". Use "\>" to solve this problem:
:%s/\<four\>/4/g
If you are coding, you may just want to replace "four" in the comment, while preserving the code. Since this is difficult to specify, you can add a "C" tag to the replacement command so that Vim prompts you before each replacement:
:%S/\<FOUR\>/4/GC
2. Remove extra spaces
To remove the extra space behind each line, you can execute the following command:
:%s/\s\+$//
The command indicates that the range is "%", so this works on the entire file. The match pattern for the "substitute" command is
"\s\+$". This represents one or more (\+) spaces (\s) before the end of the line ($). The "to" part of the Replace command is empty: "//". This will delete those matching whitespace characters.
3, matching repeatability mode
The asterisk item "*" Specifies that items in front of it can be repeated any time. So:
/a*
Match "A", "AA", "AAA", and so on. But also matches "" (empty string), because 0 times is also included. The asterisk "*" is applied only to the item that is immediately in front of it. So "ab*" matches "a", "AB", "ABB", "abbb", and so on. To repeat the entire string more than once, the string must be made up of an item. The way to form an item is to precede it with "\ (", followed by "\"). So this command:
/\ (ab\) *
Matches: "AB", "Abab", "Ababab", and so on. and also match "".
To avoid matching empty strings, use "\+". This means that the preceding item can be matched one or more times.
/ab\+
Match "AB", "ABB", "abbb", etc. It does not match "a" followed by "B".
To match an optional option, use "\=". For example:
/folders\=
Match "folder" and "Folders".
4, specify the number of repetitions
To match a specific number of times for an item, use the form "\{n,m}". where "n" and "M" are numbers. The item in front of it will be repeated "n" to "M" Times (|inclusive| contains "n" and "M"). For example:
/ab\{3,5}
Match "abbb", "abbbb" and "abbbbb".
When "n" is omitted, it is defaulted to zero. When "M" is omitted, the default is infinity. When ", M" is omitted, it means repeating exactly "n" Times. For example:
Pattern Match times
\{,4} 0,1,2,3 or 4
\{3,} 3,4,5, etc.
\{0,1} 0 or 1, same as \=
\{0,} 0 or more, same as *
\{1,} 1 or more, with \+
\{3} 3
5, multiple Select a match
In a lookup mode, the OR operator is "\|". For example:
/foo\|bar
This command matches "foo" or "bar". More choices can be attached to the back:
/one\|two\|three
Matches "One", "one", or "three".
To match its multiple repetitions, the entire decision structure must be placed between "\ (" and "\)":
/\ (foo\|bar\) \+
This command matches "foo", "Foobar", "Foofoo", "Barfoobar", and so on.
Let me give you an example:
/end\ (if\|while\|for\)
This command matches "endif", "Endwhile" and "ENDfor".
Vim shortcut key Operation command Daquan under CentOS