Vim 快捷键速查表
编辑器 vi Vim
![](/data/attachment/album/201701/25/082413qcekbeffh2m4x2fe.jpg)


本文是 Vim 用户指南 系列的其中一篇：


* [Vim 初学者入门指南](/article-8143-1.html)
* Vim 快捷键速查表
* [5 个针对有经验用户的 Vim 技巧](/article-8148-1.html)
* [3 个针对高级用户的 Vim 编辑器实用技巧](/article-8149-1.html)


Vim 编辑器是一个基于命令行的工具，是传奇编辑器 vi 的增强版。尽管图形界面的富文本编辑有很多，但是熟悉 Vim 对于每一位 Linux 的使用者都能有所帮助——无论你是经验丰富的系统管理员，还是刚上手树莓派的新手用户。


这个轻量级的编辑器是个非常强大的工具。在有经验的使用者手中，它能完成不可思议的任务。除了常规的文本编辑功能以外，它还支持一些进阶特性。例如，基于正则表达式的搜索和替换、编码转换，以及语法高亮、代码折叠等的编程特性。


使用 Vim 时有一个非常重要的一点需要注意，那就是按键的功能取决于编辑器当前的“模式”。例如，在“普通模式”输入字母`j`时，光标会向下移动一行。而当你在“插入模式”下输入字符，则只是正常的文字录入。


下面就是速查表，以便于你充分利用 Vim。


### 基本操作




| 快捷键 | 功能 |
| --- | --- |
| `Esc` | 从当前模式转换到“普通模式”。所有的键对应到命令。 |
| `i` | “插入模式”用于插入文字。回归按键的本职工作。 |
| `:` | “命令行模式” Vim 希望你输入类似于保存该文档命令的地方。 |


### 方向键




| 快捷键 | 功能 |
| --- | --- |
| `h` | 光标向左移动一个字符 |
| `j` 或 `Ctrl + J` | 光标向下移动一行 |
| `k` 或 `Ctrl + P` | 光标向上移动一行 |
| `l` | 光标向右移动一个字符 |
| `0` | （数字 0）移动光标至本行开头 |
| `$` | 移动光标至本行末尾 |
| `^` | 移动光标至本行第一个非空字符处 |
| `w` | 向前移动一个词 （上一个字母和数字组成的词之后） |
| `W` | 向前移动一个词 （以空格分隔的词） |
| `5w` | 向前移动五个词 |
| `b` | 向后移动一个词 （下一个字母和数字组成的词之前） |
| `B` | 向后移动一个词 （以空格分隔的词） |
| `5b` | 向后移动五个词 |
| `G` | 移动至文件末尾 |
| `gg` | 移动至文件开头 |


### 浏览文档




| 快捷键 | 功能 |
| --- | --- |
| `(` | 跳转到上一句 |
| `)` | 跳转到下一句 |
| `{` | 跳转到上一段 |
| `}` | 跳转到下一段 |
| `[[` | 跳转到上一部分 |
| `]]` | 跳转到下一部分 |
| `[]` | 跳转到上一部分的末尾 |
| `][` | 跳转到上一部分的开头 |


### 插入文本




| 快捷键 | 功能 |
| --- | --- |
| `a` | 在光标后插入文本 |
| `A` | 在行末插入文本 |
| `i` | 在光标前插入文本 |
| `o` | （小写字母 o）在光标下方新开一行 |
| `O` | （大写字母 O）在光标上方新开一行 |


### 特殊插入




| 快捷键 | 功能 |
| --- | --- |
| `:r [filename]` | 在光标下方插入文件 [filename] 的内容 |
| `:r ![command]` | 执行命令 [command] ，并将输出插入至光标下方 |


### 删除文本




| 快捷键 | 功能 |
| --- | --- |
| `x` | 删除光标处字符 |
| `dw` | 删除一个词 |
| `d0` | 删至行首 |
| `d$` | 删至行末 |
| `d)` | 删至句末 |
| `dgg` | 删至文件开头 |
| `dG` | 删至文件末尾 |
| `dd` | 删除该行 |
| `3dd` | 删除三行 |


### 简单替换文本




| 快捷键 | 功能 |
| --- | --- |
| `r{text}` | 将光标处的字符替换成 {text} |
| `R` | 进入覆写模式，输入的字符将替换原有的字符 |


### 复制/粘贴文本




| 快捷键 | 功能 |
| --- | --- |
| `yy` | 复制当前行至存储缓冲区 |
| `["x]yy` | 复制当前行至寄存器 x |
| `p` | 在当前行之后粘贴存储缓冲区中的内容 |
| `P` | 在当前行之前粘贴存储缓冲区中的内容 |
| `["x]p` | 在当前行之后粘贴寄存器 x 中的内容 |
| `["x]P` | 在当前行之前粘贴寄存器 x 中的内容 |


### 撤销/重做操作




| 快捷键 | 功能 |
| --- | --- |
| `u` | 撤销最后的操作 |
| `Ctrl+r` | 重做最后撤销的操作 |


### 搜索和替换




| 快捷键 | 功能 |
| --- | --- |
| `/search_text` | 检索文档，在文档后面的部分搜索 search\_text |
| `?search_text` | 检索文档，在文档前面的部分搜索 search\_text |
| `n` | 移动到后一个检索结果 |
| `N` | 移动到前一个检索结果 |
| `:%s/original/replacement` | 检索第一个 “original” 字符串并将其替换成 “replacement” |
| `:%s/original/replacement/g` | 检索并将所有的 “original” 替换为 “replacement” |
| `:%s/original/replacement/gc` | 检索出所有的 “original” 字符串，但在替换成 “replacement” 前，先询问是否替换 |


### 书签




| 快捷键 | 功能 |
| --- | --- |
| `m {a-zA-Z}` | 在当前光标位置设置书签，书签名可用一个大小写字母（{a-zA-Z}） |
| `:marks` | 列出所有书签 |
| `{a-zA-Z}` | 跳转到书签 {a-zA-Z} |


### 选择文本




| 快捷键 | 功能 |
| --- | --- |
| `v` | 进入逐字可视模式 |
| `V` | 进入逐行可视模式 |
| `Esc` | 退出可视模式 |


### 改动选中文本




| 快捷键 | 功能 |
| --- | --- |
| `~` | 切换大小写 |
| `d` | 删除一个词 |
| `c` | 变更 |
| `y` | 复制 |
| `>` | 右移 |
| `<` | 左移 |
| `!` | 通过外部命令进行过滤 |


### 保存并退出




| 快捷键 | 功能 |
| --- | --- |
| `:q` | 退出 Vim，如果文件已被修改，将退出失败 |
| `:w` | 保存文件 |
| `:w new_name` | 用 new\_name 作为文件名保存文件 |
| `:wq` | 保存文件并退出 Vim |
| `:q!` | 退出 Vim，不保存文件改动 |
| `ZZ` | 退出 Vim，如果文件被改动过，保存改动内容 |
| `ZQ` | 与 :q! 相同，退出 Vim，不保存文件改动 |


### 下载 Vim 快捷键速查表


仅仅是这样是否还不足以满足你？别担心，我们已经为你整理好了一份下载版的速查表，以备不时之需。


[点此下载（英文）](http://www.maketecheasier.com/cheatsheet/vim-keyboard-shortcuts-cheatsheet/)




---


via: <https://www.maketecheasier.com/vim-keyboard-shortcuts-cheatsheet/>


作者：[Himanshu Arora](https://www.maketecheasier.com/author/himanshu/) 译者：[martin2011qi](https://github.com/martin2011qi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Vim Cheatsheet 00 Featured Image](https://www.maketecheasier.com/assets/uploads/2022/05/vim-cheatsheet-00-featured-image.jpg)

Vim is a powerful command-line code editor tool that’s an enhanced version of the venerable vi editor. Although most people use it with Linux, Vim is compatible with most of the commonly used operating systems, including macOS, Windows, and DOS.

Despite the abundance of graphical rich [text editors](https://www.maketecheasier.com/linux-text-editors/), familiarity with Vim will help every user, from an experienced system administrator to a newbie programming a [Raspberry Pi](https://www.maketecheasier.com/turn-raspberry-pi-video-conferencing-station/). However, one important thing to note when using Vim, is that the function of a key depends on the “mode” the editor is in. For example, pressing the alphabet “j” will move the cursor down one line in the “command mode”. You’ll have to switch to the “insert mode” to make the keys input the character they represent.

If you’re new to Vim, open a terminal and run `vimtutor`

to get you started with some initial commands before diving into the rest.

![VIM Keyboard Shortcuts Cheatsheet Download](https://www.maketecheasier.com/assets/uploads/2022/07/Vim-Keyboard-Shortcuts-Cover-Image-1.jpeg)

#### Download this Cheatsheet

Enter your email below to receive this PDF cheatsheet in your Inbox.

Here’s a cheatsheet to help you get the most out of Vim.

Shortcut Keys | Function |
---|---|

Main | |

Esc | Gets out of the current mode into the “command mode”. All keys are bound of commands. |

I | “Insert mode” for inserting text. Keys behave as expected. |

: | “Last-line mode” where Vim expects you to enter a command such as to save the document. |

:term | Open a terminal window. |

Navigation Keys | |

H | Move the cursor one character to the left. |

J or Ctrl + J | Move the cursor down one line. |

K or Ctrl + P | Move the cursor up one line. |

l | Move the cursor one character to the right. |

0 | Move the cursor to the beginning of the line. |

$ | Move the cursor to the end of the line. |

^ | Move the cursor to the first non-empty character of the line. |

w | Move forward one word (next alphanumeric word). |

W | Move forward one word (delimited by a white space). |

5W | Move forward five words. |

b | Move backward one word (previous alphanumeric word). |

B | Move backward one word (delimited by a white space). |

5B | Move backward five words. |

G | Move to the end of the file. |

GG | Move to the beginning of the file. |

Navigate Around The Document | |

( | Jump to the previous sentence. |

) | Jump to the next sentence. |

{ | Jumps to the previous paragraph. |

} | Jumps to the next paragraph. |

[[ | Jumps to the previous section. |

]] | Jumps to the next section. |

[] | Jump to the end of the previous section. |

][ | Jump to the end of the next section. |

Insert Text | |

a (lowercase A) | Insert text after the cursor. |

A (uppercase A) | Insert text at the end of the line. |

I | Insert text before the cursor. |

o (lowercase O) | Begin a new line below the cursor. |

O (uppercase A) | Begin a new line above the cursor. |

Special Inserts | |

:R [filename] | Insert the file [filename] below the cursor. |

:R ![command] | Execute [command] and insert its output below the cursor. |

Delete Text | |

X | Delete character at cursor. |

DW | Delete a word. |

D0 | Delete to the beginning of a line. |

D$ | Delete to the end of a line. |

D) | Delete to the end of sentence. |

DGG | Delete to the beginning of the file. |

DG | Delete to the end of the file. |

DD | Delete line. |

3DD | Delete three lines. |

Simple Replace Text | |

R{text} | Replace the character under the cursor with {text}. |

R | Replace characters instead of inserting them. |

Copy/Paste Text | |

YY | Copy current line into storage buffer. |

[“x]yy | Copy the current lines into register x. |

p (lowercase P) | Paste storage buffer after current line. |

P (uppercase A) | Paste storage buffer before current line. |

[“X]P | Paste from register x after current line. |

[“X]P | Paste from register x before current line. |

Text Modification | |

CC | Change the text of the current line. |

CE | Change the currently selected word at the current cursor point. |

C$ | Change the text from the cursor position until the end of the line. |

C0 | Change the text from the cursor position until the start of the line. |

G~ | Switch the case of the currently selected text as well as the line adjacent to it to its opposite. |

Gu | Change the case of the currently selected text as well as the line adjacent to it to lowercase. |

GU | Change the case of the currently selected text as well as the line adjacent to it to uppercase. |

Context-Specific Modification | |

CIW | Change the currently selected word regardless of cursor position. |

CIP | Change the currently selected paragraph or code block regardless of cursor position. |

CI< | Change the content of a code block enclosed in angle brackets (<>). |

CI{ | Change the content of a code block enclosed in curly brackets ({}). |

DIW | Delete the currently selected word regardless of cursor position. |

DIP | Delete the currently selected paragraph or code block regardless of cursor position. |

DAW | Delete the currently selected word as well as the spaces before and after it. |

DI< | Delete the content of a code block enclosed in angle brackets (<>). |

DI{ | Delete the content of a code block enclosed in curly brackets ({}). |

YIW | Copy the currently selected word regardless of cursor position. |

YIP | Copy the currently selected paragraph or code block regardless of cursor position. |

YAW | Copy the currently selected word as well as the spaces before and after it. |

YI< | Copy the content of a code block enclosed in angle brackets (<>). |

YI{ | Copy the content of a code block enclosed in curly brackets ({}). |

Undo/Redo Operation | |

U | Undo the last operation. |

Ctrl + R | Redo the last undo. |

:earlier 1m | Go back 1 minute in the history of the document. |

:later 1m | Go forward 1 minute in the history of the document. |

Search and Replace Keys | |

/search_text | Search document for search_text going forward. |

?search_text | Search document for search_text going backward. |

n (lowercase n) | Move to the next instance of the result from the search. |

N (uppercase A) | Move to the previous instance of the result. |

:%s/original/replacement | Search for the first occurrence of the string “original” and replace it with “replacement”. |

:%s/original/replacement/g | Search and replace all occurrences of the string “original” with “replacement”. |

:%s/original/replacement/gc | Search for all occurrences of the string “original” but ask for confirmation before replacing them with “replacement”. |

F | Search for the next occurrence of a character or go to the previous occurrence. |

Bookmarks | |

M {a-z A-Z} | Set bookmark {a-z A-Z} at the current cursor position. |

:marks | List all bookmarks. |

`{a-z A-Z} | Jumps to the bookmark {a-z A-Z}. |

Select Text | |

v (lowercase V) | Enter visual mode per character. |

V (uppercase A) | Enter visual mode per line. |

Esc | Exit visual mode. |

Modify Selected Text | |

~ | Switch case. |

D | Delete a word. |

C | Change a word. |

Y | Yank (copy) a word. |

> | Shift right. |

< | Shift left. |

! | Filter through an external command. |

Text Indentation | |

>> | Shift the currently selected line one Tab length to the right. |

<< | Shift the currently selected line one Tab length to the left. |

gg=G | Shift the current buffer by one Tab length. |

>% | Shift the currently selected code block to the right. |

<% | Shift the currently selected code block to the left. |

>IB | Shift the contents of the currently selected block to the right. |

<IB | Shift the contents of the currently selected block to the left. |

>AT | Shift the contents of a block that is enclosed in angle brackets (<>) to the right. |

<AT | Shift the contents of a block that is enclosed in angle brackets (<>) to the left. |

Macros | |

Q[character] | Record a Vim macro that is accessible under the [character] key. |

Q | Stop a Vim macro recording. |

@[character] | Execute a recorded Vim macro that was saved under the [character] key. |

@@ | Execute the last Vim macro that was run. |

Search and Execute | |

:G/[pattern]/[vim command] | Run the [vim command] in the current line for every instance of the [pattern]. |

:G!/[pattern]/[vim command] | Run the [vim command] in the current line for every instance that is not the [pattern]. |

:%G/[pattern]/[vim command] | Run the [vim command] on the entire document for every instance of the [pattern]. |

:%G!/[pattern]/[vim command] | Run the [vim command] on the entire document for every instance that is not the [pattern]. |

Tab Manipulation | |

:TABE [file path] | Open the [file path] in a separate tab in the current buffer. |

:TABN | Switch to the next active tab in the buffer. |

:TABP | Switch to the previous active tab in the buffer. |

:TABC | Close the currently active tab in the buffer. |

:TABO | Close all inactive tabs in the buffer. |

Buffer Manipulation | |

:SP [file path] | Open the [file path] as a horizontally split buffer. |

:VSP [file path] | Open the [file path] as a vertically split buffer. |

:LS | Show a list of all the buffers currently open in Vim. |

:BN | Display the contents of the next open buffer in the buffer list. |

:BP | Display the contents of the previous open buffer in the buffer list. |

Ctrl + W, then S | Split the current window horizontally, displaying the currently active buffer in both windows. |

Ctrl + W, then V | Split the current window vertically, displaying the currently active buffer in both windows. |

Ctrl + W, then W | Switch focus between the split buffers. |

Ctrl + W, then X | Swap the position of the currently focused buffer with an inactive one. |

Ctrl + W, then Q | Delete the currently selected buffer. |

Save and Quit | |

:Q | Quits Vim but fails when file has been changed. |

:W | Save the file. |

:W new_name | Save the file with the new_name filename. |

:WQ | Save the file and quit Vim. |

:Q! | Quit Vim without saving the changes to the file. |

ZZ | Write file, if modified, and quit Vim. |

ZQ | Same as :q! Quits Vim without writing changes. |

:sav file :saveas file | Save file as. |

:clo :close | Close the current pane. |


Image credit: Unsplash

Our latest tutorials delivered straight to your inbox