vim Three common patterns
(Normal mode, edit mode, command mode)
General mode
Definition: When we use the command vim filename when you edit the file. The default normal mode to enter the file. In this mode, you can: move the cursor up and down, delete a character, delete a row and copy one or more rows.
H (left direction key): cursor one character to the left
L (right direction key): Move the cursor one character to the right
K (the direction of the bond): move the cursor up one character
J (the key direction key): move the cursor down one character
B + Ctrl : turn a text page forward
F + Ctrl : turn back a page of text
Number 0 or Shift +. 6 : Bank moved to the first row
+. 4 Shift : move to the end of the line Bank
GG : move to the first line
G : move to the end of the line
nG ( n is an arbitrary number): move to the first n row
x and X: x represents the after want to delete a character, the X- delete a character forward
NX : Delete backward n characters
dd Delete: / which line the cursor shear
NDD : Delete / cut after the cursor of the row n rows
YY : copy cursor line
P : starting from the cursor line, the paste content has been copied or pasted down
P :: cursor from the start line, the paste content has been copied or pasted up
NYY : starting from the cursor line, copy down n rows
u: undo the last operation
v : Press v move the cursor to select the specified character, and can realize copy, and paste operations etc.
Edit mode
Definition: can not change a character in a normal mode, if you want to modify character, can only enter edit mode. Enter the edit mode from the general mode, simply press I , the I , A , A , O , O , R & lt and R one can. Return to normal mode simply press esc .
i : before the current character insertion
The I : first insert the cursor line line
A : After this current character insertion
A:在光标所在行的行尾插入
o: 在当前的下一行插入新的一行
O:在当前的上一行插入新的一行
命令模式
定义:在一般模式下,输入:或者/即可进入命令模式。在该模式下,可以搜索某个字符或者字符串,也可以实现保存、替换、退出、显示行号等操作。
/word 在光标之后查找一个字符串word,按n向后继续搜索
?word 在光标之前查找一个字符串word,按n向前继续搜索
:n1,n2s、/word1/word2/g 在n1和n2行之间查找并替换为word,不加则只替换每行的第一个word1
:1,$/word1/word2/g 将文档中所有的word1替换为word2,不加则只替换每行的第一个word1
:q 退出vim
:w!强制保存,在root用户下,即使文本只读也可以完成保存
:q! 强制退出,所有改动不生效
:wq 保存并退出
:set nu 显示行号
:set nonu 不显示行号