Common Vim commands

Source: Internet
Author: User
Tags repetition

1. Switch the insert mode:
In command mode
Insert after a cursor
Insert at the end of the row where the cursor is located
I start inserting before the cursor
Insert at the beginning of the line where the cursor is located
O insert an empty row after the cursor row
O insert an empty row before the row where the cursor is located
Press ESC in insert mode to return to command mode
2. Save the file
In command mode
: W save the file
: WQ: Save and exit Vim
: W file name 1 save the file as file name 1
: ZZ saves the file and exits, equivalent to: WQ

: X saves the file and exits, which is equivalent to: WQ
: WQ! The file owner forces to save the read-only file. If the file owner does not perform this operation, the operation cannot be successful.
: Q! Exit and do not save the file

3 copy (Yank)
in command mode,
YY or Y: copy the entire line where the cursor is located
y ^ to the beginning of the line where the cursor is located, the cursor position character is not included (only the string is copied and there is no line break, so it is pasted in the row where the cursor is located)
Y $ copy to the end of the row where the cursor is located, including the cursor position character
YW copies a word, and the cursor must be in the word header (if it is not in the word header, copy the character where the cursor is located to the end of the word, such as what, if the light is calibrated at H, copy hat)
copy YG to the end of the file (the row where the cursor is located to the last row of the file, including the row where the cursor is located)
copy y1g to the beginning of the file (the row where the cursor is located to the last line of the file, including the row where the cursor is located)
nyy copies the N rows starting from the row where the cursor is located
4 paste
in command mode
P (lower case) paste to the cursor
P (uppercase) paste to the front of the cursor


5. Delete
In command mode
X (lower case) deletes the current cursor character, that is, the row where the cursor is located
X (uppercase) deletes the character before the cursor
Dd (lowercase) deletes the entire row where the cursor is located
DW (lower case) deletes a word where the cursor is located, and deletes the remaining part of the word after the cursor. The next word is automatically moved forward, occupying the position of the deleted part.
DG Delete the entire row where the cursor is located to the end of the file
DGG deletes the entire row where the cursor is located to the beginning of the file
D (uppercase) is deleted to the end of the line, containing the characters at the cursor
D $ has the same effect as d
D0 is deleted to the beginning of the line, excluding the characters at the cursor
D ^ has the same effect as D0
: N1, n2d command mode, delete the n1-n2 line


6. Undo
In command mode

U (lower case) undo before Infinite save (about 500 times)

CTRL + R redo

7-character replacement
In command mode
R (lower case) replaces the cursor with a character
R (uppercase) begins to replace, until you Press ESC to exit the replacement, the equivalent of pressing the keyboard to Insert key
CC (lower case) replaces the entire line where the cursor is located
S (uppercase) has the same effect as SS (lowercase)
C (uppercase) is replaced by the end of the line, including the character at the cursor
C0 (lowercase, number 0) is replaced by the first line, excluding the characters at the cursor
C ^ (lower case) and C0 (lower case, number 0) have the same effect


8. search and replace strings
In command mode
/String start from the cursor and start to look down the string
/In the search mode, search for the next one by N (lower case) and the last one by N (upper case ).
? String starts from the cursor and starts to look up the character from string
? In search mode, click N (lower case) to find the previous one, and then click N (upper case) to find the next one.
* Completely match words under the cursor
# Match words with the cursor up and down
G * match words under the cursor in the lower part
G # match the cursor to the word

: Case Insensitive when searching for set IC
: Case sensitivity is ignored when set noic is unsearched.
: F String (lower case F and string have spaces) search string matching in a row
: % S/old/new/g replace old with new in full text, no prompt
: % S/old/new/C replace old with new in full text, prompting whether to replace

: S/old/new/g the cursor will replace old with new in the current line. No prompt is displayed.
: S/old/new/C the cursor will replace old with new in the current line, prompting whether to replace
: N1, N2s/old/new/g n1-n2 line, replace old with new, do not prompt
: N1, N2s/old/new/C n1-n2 line, replace old with new, prompting whether to replace
Escape with \ when replacing the old or new text with/characters


9 show row number
In command mode
: Set nu (mber) displays the row number
: Set Nonu undisplay row number
: Set Nu! Unshow row number

10 simple layout
In command mode
: Ce (NTER) center displays the row where the cursor is located
: RI (ght) Right-click to show the row where the cursor is located
: Le (FT) to the left to show the row where the cursor is located
In command mode
J. Merge the next row where the cursor is located into the row where the cursor is located.
> The line where the cursor is located is indented (one tab)
<Reduce indentation of the row where the cursor is located (one tab)
N> N rows starting with the row where the cursor is located add indentation
N <n rows starting from the row where the cursor is located reduce indentation

11 move the cursor
In command mode
H (uppercase, head) Move to the first non-blank character on the top of the screen
M (uppercase, mid) moves to the first non-blank character in the middle of the screen
L (uppercase, last) Move to the first non-blank character at the bottom of the screen
(Left parentheses) Move to the beginning of the previous sentence
) (Right parentheses) Move to the beginning of the next sentence
{(Left braces) Move to the beginning of the previous paragraph
} (Right braces) Move to the beginning of the next paragraph
% The cursor jumps to the matching brackets. {} () is supported {}()
[[Jump the cursor to the beginning of the code block, that is, {, requires {exclusive line
GD cursor jump to local variable definition
''(Two single quotes) the cursor jumps to the last dock
H (lower case) move the cursor one character to the left, which is equivalent to the left arrow key.
L (lower case) move the cursor one character to the right, which is equivalent to the right arrow key.
K (lower case) move the cursor up vertically, which is equivalent to the up arrow key.
J (lower case) the cursor moves down a line vertically, which is equivalent to the lower direction key.
CTRL + f (forword) page flip
CTRL + B (backward) page up
CTRL + U (up) up half page
CTRL + d (down) Flip down half page
ZZ (lower case) Place the cursor row in the center of the screen
Zt (lower case) Place the cursor row at the top of the screen
ZB (lower case) places the cursor row at the bottom of the screen
In command mode
: N indicates moving to row n.


12. Number prefix and repetition
In command mode
Add the number before the command to indicate how many times the command is processed, for example, 5dd indicates that the operation is performed five times dd (Delete the entire line where the cursor is located.
. (English period) is the command repetition command, and the last command that has been executed once is pressed.

13 browsing directory
In command mode
: E Directory: List objects and directories under a specified directory


14 Vim skills
tip 1 import file or shell command execution results
in command mode
: r file name 1 introduce file 1 to this file (insert to the end of the row where the cursor is located)
:! Run the shell command in Vim. After the command is executed, press enter To Go To The Vim interface
: R! Shell commands (R and! There is a space between them) import the results of shell command execution to this file
Tip 2 custom shortcut key operation
in editing mode
: map ^ x command (there is a space between map and ^, and between x and the command. Here to ^ is not the ^ on the keyboard, but press Ctrl + V to display the shortcut key, which indicates the ctrl key, press Ctrl + X to execute the corresponding command
example:
: map ^ p I # when you press Ctrl + P, add a # sign at the beginning of the row where the cursor is located, return to command mode
Tip 3 continuous line comment
edit mode
: N1, N2/^/#/g (# Is the annotation symbol, in shell, the annotation symbol is #, C ++ is //)
: N1, n2/^/\/g C ++ source file multi-row continuous comments
Tip 4 replace
: after AB string1 string2 is entered in vim and string1 is pressed by space or press enter, string1 is automatically replaced with string2
: unab String1 cancel replacement of string1
Vim configuration file
~ /. Vimrc
you can write the commands in the editing mode, and each command is a single line. In this way, the commands in the command are automatically executed each time you start it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.