vi and vim shortcut key description Daquan

1 General command mode

1.1 Moving the cursor

button illustrate
h or left arrow key Move the cursor one character to the left
j or down arrow key Move the cursor down one character
k or up arrow key Move the cursor up one character
l or right arrow key Move the cursor one character to the right
[ctrl]+[f] The screen moves down one page, equivalent to [Page Down] (commonly used)
[ctrl]+[b] The screen moves up one page, equivalent to [Page Up] (commonly used)
[ctrl]+[d] Move the screen down by half a page
[ctrl]+[u] Move the screen up by half a page
n+[Space] n represents a number, press the number and then press the space bar, the cursor will move to the right to the nth character of this line.
Function key [Home] or 0 Move the cursor to the first character of the line
Function key [End] or $ Move the cursor to the last character of the line (commonly used)
H Move the cursor to the first character of the line at the top of the screen
M Move the cursor to the first character of the line in the center of this screen
L Move the cursor to the first character of the bottom line of this screen
G Move the cursor to the last line of this file (commonly used)
nG n is a number. Move cursor to line n of this file
gg Move the cursor to the first line of this file (commonly used)
n+[Enter] n is a number. Move the cursor down n rows (commonly used)

1.2 Search and replace

button illustrate
/word Look down at the current cursor position to find the string with the keyword word (commonly used)
?word Look up the string with the keyword word at the current cursor position
n Here n is the key n. Represents repeating the previous search action
N Here N is key N. The function is the opposite of n, representing the reverse execution of the previous search action
:n1,n2s/word1/word2/g Both n1 and n2 are numbers, indicating the number of rows. The word1 string can be found between lines n1 and n2 and replaced with word2 (common)
:1,$s/word1/word2/g Indicates that from the first line to the last line, replace the word1 string with word2 (commonly used)
:1,$s/word1/word2/gc Indicates that from the first line to the last line, it also replaces the word1 string with word2, the difference is that the prompt character is displayed before the replacement, and the user confirms whether the final replacement (commonly used)

* When using /word, combined with n or N, the search can become very convenient :)

1.2 Delete, copy and paste

button illustrate
[Del] or x Delete one character backward (common)
X Delete one character forward (common)
nx n is a number, which means to delete n characters in a row
dd Delete the row where the cursor is located (commonly used)
ndd n is a number, delete n rows from the row where the cursor is located (commonly used)
d1G Delete all data from the row where the cursor is located to the first row
dG Delete all data from the row where the cursor is located to the last row
d$ Delete the cursor to the last character of the row
d0 is the number 0, delete the cursor to the first character of the row
yy Copy the row where the cursor is located (commonly used)
nyy n is a number, copy the row where the cursor is located and count down n rows (commonly used)
y1G Copy all data from the row where the cursor is located to the first row
which Copy all data from the row where the cursor is located to the last row
y0 Copy the character where the cursor is located to all the data at the beginning of the line
y$ Copy the character where the cursor is located to all data at the end of the line
p Paste the copied data in the row below the row where the cursor is located (commonly used)
P 将已复制的数据粘贴在游标所在的那一行的上一行,原来游标所在的那一行会被推后一行 (常用)
J 将游标所在的行与下一行的数据合并为一行
u 撤销前一个操作 (常用)
[ctrl]+r 重做上一个被撤销的操作 (常用)
. 这是小数点,表示重复前一个操作 (常用)

* 最后三个按键乃是神器,熟悉后记得经常使用它们,会让你觉得使用 vim 是一件很快乐的事情哦 :)

2 一般指令模式切换到编辑模式

按键 说明
i 进入插入模式;从目前游标所在处插入
I 进入插入模式;从目前游标所在行的第一个非空白字元处插入 (常用)
a 进入插入模式;从目前游标所在处的下一个字元开始插入
A 进入插入模式;从目前游标所在行的最后一个字元开始插入 (常用)
o 是字母,表示进入插入模式;从目前游标所在行的下一行处插入新的一行
O 是字母,表示进入插入模式;从目前游标所在行的上一行处插入新的一行 (常用)
r 进入取代模式;只会取代游标所在的那个字元一次
R 进入取代模式;会一直取代游标所在的文字,直到按下 [Esc] 为止 (常用)
[Esc] 从编辑模式返回到一般指令模式 (常用)

* 在左下角处看到 INSERT 或 REPLACE 时,才表示已经进入编辑模式。

3 一般指令模式切换到指令列模式

按键 说明
:w 将编辑的文件写入硬盘 (常用)
:w! 如果文件为只读状态,可以强制写入硬盘,是否写入成功,与你所拥有的文件权限有关
:q 离开 vi (常用)
:q! 即使文件未保存,仍强行离开 vi
:wq 文件写入硬盘后(即保存)离开 vi (常用)
:wq! 文件强制写入硬盘后(即保存)离开 vi
ZZ 大写字母 Z,如果文件没有修改,则直接离开;如果文件被修改,则保存后离开
:w [filename] 将当前文件另存为另一个文件,这个新文件的保存路径为当前路径
:r [filename] 在当前文件中读入另一个文件的数据,插入到当前游标所在列的后面
:n1,n2 w [filename] 将第 n1 行到第 n2 行的数据储存为新的文件
:! command 暂时离开 vi 到指令列模式下执行 command 的显示结果

* ! 表示强制执行

4 vim 特有功能

4.1 区域选择

按键 说明
v 字符选择,移动游标,游标经过的地方会反白选择
V 行选择,移动游标,游标经过的行会反白选择
[Ctrl]+v 区块选择,移动游标以长方形方块的方式反白选择
y 复制反白选择的地方
d 删除反白选择的地方
p 将之前复制的区块,粘贴在游标所在处

4.2 多文件编辑

按键 说明
:n 编辑下一个文件
:N 编辑上一个文件
:files 列出 vim 所打开的所有文件

使用 vim a.txt b.txt … 指令,就能一次打开多个文件

4.3 多视窗

按键 说明
:sp [filename] 开启一个新视窗。如果有加 filename,表示是在新视窗开启一个新文件;否则,表示两个视窗为同一个文件的内容(即同步显示)
[ctrl]+w+向下方向键 先按下[ctrl]不放,然后再按下 w 后放开所有按键,最后再按下向下方向键,游标就会移动到下方的视窗
[ctrl]+w+向上方向键 按键方法同上,游标会移动到上方的视窗
[ctrl]+w+q 关闭当前游标所在的视窗

4.4 补齐

按键 说明
[ctrl]+x -> [ctrl]+n 依据目前正在编辑的文件内容作为关键字,予以补齐(语法依据副档名)
[ctrl]+x -> [ctrl]+f 依据当前目录下的文件名,予以补齐
[ctrl]+x -> [ctrl]+o 以副档名作为语法关键字,予以补齐

4.5 环境设定参数

按键 说明
:set nu 设定行号
:set nonu 取消行号
:set hlsearch 搜索到的关键字反白显示 (默认)
:set nohlsearch 搜索到的关键字取消反白显示
:set autoindent 自动缩排 (默认)
:set noautoindent 不自动缩排
:set backup 自动存储备份档(备份档名为 filename~)
:set ruler 显示右下角状态说明
:set showmode 显示左下角状态说明(比如 INSERT)
:set backspace=(012) 倒退键设置;2:可以删除任意值;0 或者 1,仅可删除刚刚输入的字符,无法删除原来就存在的字符
:set all 显示所有环境参数设定值
:set 显示与系统默认值不同的设定值
:syntax on 开启语法着色
:syntax off 关闭语法着色
:set bg=dark 开启语法着色深色方案
:set bg=light 开启语法着色浅色方案 (默认)

log:
* 17-01-16 add 补齐、环境设定参数
* 17-01-11 add 多文件编辑、多视窗
* 17-01-09 add 一般指令模式切换到指令列模式、区域选择
* 17-01-04 add 一般指令模式切换到编辑模式
* 16-12-30 new

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811931&siteId=291194637