命令模式快捷键
键入 Esc
进入命令模式(Command mode)
快捷键是区分大小写的,大写表示需要按下 shift
按键。命令不会全部给出,要学会举一反三!
此外,vim 中的光标的形状不是细线,而是块状(block),所以“光标所在字符”很容易理解,但“光标后”和“光标前”的含义,就得好好注意了,因为它可能和你以为的含义有所出入!
motion 光标移动
shortcuts | info | 备注 |
---|
+ | lines upward, on the first non-blank character | 跳到下一行的非空字符 |
h | Moving the cursor to the left | 左 |
j | Moving the cursor down | 下 |
k | Moving the cursor up | 上 |
l | Moving the cursor to the right right | 右 |
gg | Beginning of buffer | 文件首行 |
G | End of buffer | 文件末行 |
<line-number>G | Navigate to a specific line | 指定行 |
20gg | | 跳到 20 行 |
^ | Beginning of line | 行首 |
$ | End of line | 行尾 |
{ | Move the cursor to the beginning of the previous paragraph | 上一个段落 |
} | Move the cursor to the beginning of the next paragraph | 下一个段落 |
w | Move the cursor forward to the beginning of the next word | 下一个 word |
b | Move the cursor backward to the beginning of the previous word | 上一个 word |
W | Move the cursor forward to the beginning of the next WORD (a WORD is delimited by whitespace) | 下一个 foo-bar-baz |
B | Move the cursor backward to the beginning of the previous WORD | 上一个 foo-bar-baz |
Ctrl+o | Jump backward to the previous cursor position in the jump list | 光标回退 |
Ctrl+i | Jump forward to the next cursor position in the jump list | 光标前进 |
复制 + 粘贴 + 剪切
shortcuts | info | 备注 |
---|
p | (Paste) Paste from clipboard after the cursor position | 粘贴至光标后(或下一行)。“光标后”指的是下一个光标所在位置,若光标在行末,则相当于往行尾添加内容 |
P | (Paste) Paste from the clipboard before the cursor position | 粘贴至光标前(或上一行)。“光标前”指的是当前光标所在位置,若光标在行末,则相当于往行尾最后一个字符前添加内容 |
gp | (Paste) Paste content after cursor and leave cursor after newly added text | 同 p ,但光标放在新插入的文本后面:原本是在新插入文本的最后一个字符(或行首),但现在会再往后移动一个位置(或下一行首)。 |
gP | (Paste) Paste content before cursor and leave cursor after newly added text | 同 P ,但光标放在新插入的文本后面:原本是在新插入文本的最后一个字符(或行首),但现在会再往后移动一个位置(或下一行首——停留在原本所在行)。 |
x | (Cut) Delete character under the cursor | 剪切光标所在字符 |
2x | (Cut) Delete two character under the cursor | 剪切光标所在字符 + 下一个字符 |
X | (Cut) Delete character before the cursor | 剪切光标前一个字符 |
2X | (Cut) Delete character before the cursor | 剪切光标前一个字符 + 前前一个字符 |
dd | (Cut) Delete line | 剪切当前行 |
2dd | (Cut) Delete two line | 剪切当前行 + 下一行 |
d$ / D | (Cut) Delete text from cursor to end of line | 剪切当前光标后所有内容(一行) |
2d$ / 2D | (Cut) Delete text from cursor to end of next line | 剪切当前光标后所有内容(两行) |
d^ | (Cut) Delete text from start of line to cursor | 剪切当前光标前所有内容(仅操作一行) |
dG | (Cut) Delete text from the current line to the end of the file | 剪切当前光标所在行和后面所有内容(直至文件末尾) |
d1G | (Cut) Delete text from the start of the file to the current line | 剪切当前光标所在行和前面所有内容(直至文件开头) |
dw | (Cut) Delete the characters of the word selected from the cursor | 剪切当前光标后的 word |
daw | (Cut) Delete the current word along with surrounding whitespace | 剪切当前光标所在的 word(包括空格) |
diw | (Cut) Delete the contents of the current word without including surrounding whitespace | 剪切当前光标所在的 word(不包括空格) |
yl | (Copy) Yank the character under the cursor | 复制当前光标所在字符 |
yh | (Copy) Yank the character before the cursor | 复制当前光标左边所在字符,并向左移动 |
yy | (Copy) Yank the entire current line | 复制当前行 |
2yy | (Copy) Yank two lines | 复制当前行 + 下一行 |
yw | (Copy) Yank from the current cursor position to the end of the current word | 复制当前光标后的 word |
yaw | (Copy) Yank the words selected with the cursor and add space | 复制当前光标所在 word(包括空格) |
yiw | (Copy) Yank the words below the cursor | 复制当前光标所在 word(不包括空格) |
在命令模式中的复制粘贴剪切的内容,并不会带入到编辑模式中。
撤销和恢复
shortcuts | info | 备注 |
---|
u | (Undo) | 撤销 |
Ctrl+r | (Redo) | 重做 |
2u | (Undo) Reverts the last two changes | 推荐连按两下 u |
U | (Undo) Reverts all changes made on the line where the last change occurred | 撤销或恢复最后一次发生变更的行的所有变更 |
快捷编辑操作
shortcuts | info | 备注 |
---|
~ | Toggle the case of the character under the cursor. | 切换当前光标所在字符的大小写 |
>> | Indents the current line by one shift width | 向左缩进 |
<< | Un-indents the current line by one shift width | 向右缩进 |
r<char> | Replacing only one character with <char> | 将当前光标所在字符替换为特定字符 |
J | Joining below the line with the current one using space | 用空格将下一行和当前行合并为一行 |
gJ | Joining below the line with the current one without adding any space | 将下一行和当前行直接合并(无空格) |
ddp | (Tip) Swap the current line and the next line | 技巧:交换当前行和下一行 |
xp | (Tip) Transposing two letters | 技巧:交换当前字符和右边字符 |
Esc+o | (Tip) Insert Line Below | 技巧:向下新建一行 |
Esc+O | (Tip) Insert Line Above | 技巧:向上新建一行 |
其他
shortcuts | info | 备注 |
---|
ZZ | Write current file, if modified, and quit | 保存并退出 |
Ctrl+c | | 取消当前命令 |
d+Backspace | | 删除一个字符 |
编辑模式
shortcuts | info | 备注 |
---|
i | Insert text before the cursor | 进入输入模式 |
I | Insert text from the beginning of the line | 光标移到到行首,并进入输入模式 |
a | Append text after the cursor | 光标右移一位,并进入输入模式 |
A | Append text at the end of the line | 光标移到到行尾,并进入输入模式 |
R | Enter Replace mode | 进入替换模式(通过键盘按键 insert 可切换回输入模式) |
o | Insert text in a new line below the cursor | 向下新建一行,并进入输入模式 |
O | Insert text in a new line above the cursor | 向上新建一行,并进入输入模式 |
cc | (Cut) Changing the entire line | 剪切当前行所有内容(不含末尾换行符),并进入输入模式 |
C | (Cut) Changing from current cursor to the end of a line | 剪切当前光标后面的内容,并进入输入模式 |
cw / ce | (Cut) Replacing at the end of a word | 剪切当前光标后的 word,并进入输入模式 |
ciw | (Cut) Replacing an entire word | 剪切当前光标所在 word,并进入输入模式 |
s | (Cut) Deleting characters along with substituting its text | 剪切当前光标所在字符,并进入输入模式 |
S | (Cut) Deleting lines along with substituting their text | 同 cc |
Ctrl+c | | 退出输入模式 |
底线命令模式
键入 Esc
+ :
可进入底线命令模式(Last line mode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| :x
# 保存并退出,等同 ZZ
:w
# write file。保存
:w <new_filename>
# 将文件另存为其他文件名
:q
# 退出
:q!
# (不保存)强制退出
:qa!
# 放弃所有更改并退出
:wq
# 保存并退出,几乎等同 :x
:wq!
# 强制保存退出
:set nu
:set number
# 显示行号(number)
:set nonu
:set nonumber
# 取消行号()
:ce
# 使本行内容居中
:ri
# 使本行文本靠右
:le
# 使本行内容靠左
:/<string>
# 向光标之下寻找 <string>
:?<string>
# 向光标之上寻找 <string>
:n
# 重复前一个搜寻的动作
:1,$s/<str1>/<str2>/g
:$s/<str1>/<str2>/g
# 从第 1 行到最后一行寻找 <str1> ,并将其替换为 <str2>
|
参考