Vim

Mode Switching

i           " insert mode before cursor
a           " insert mode after cursor
I           " insert mode at line beginning
A           " insert mode at line end
o           " open new line below, enter insert
O           " open new line above, enter insert
v           " visual mode (character)
V           " visual mode (line)
Ctrl+v      " visual mode (block)
Esc         " back to normal mode

Cursor Movement

h j k l             " left, down, up, right
w                   " next word start
b                   " previous word start
e                   " word end
0                   " line beginning
^                   " first non-blank character
$                   " line end
gg                  " first line
G                   " last line
5G                  " go to line 5
%                   " jump to matching bracket
fx                  " next occurrence of x on line

Cut, Copy & Paste

dd                  " delete (cut) line
3dd                 " delete 3 lines
dw                  " delete word
d$                  " delete to end of line
D                   " delete to end of line (same as d$)
x                   " delete character under cursor
X                   " delete character before cursor

yy                  " yank (copy) line
3yy                 " yank 3 lines
yw                  " yank word
y$                  " yank to end of line

p                   " paste after cursor
P                   " paste before cursor

Undo & Repeat

u                   " undo
Ctrl+r              " redo
.                   " repeat last change

Text Objects

diw                 " delete inner word
daw                 " delete around word
di"                 " delete inside double quotes
da"                 " delete around double quotes
di(                 " delete inside parentheses
da{                 " delete around braces
ciw                 " change inner word
ci"                 " change inside quotes
yiw                 " yank inner word
vit                 " select inner HTML tag
vat                 " select around HTML tag

Windows & Tabs

:sp file.txt        " horizontal split
:vsp file.txt       " vertical split
Ctrl+w h/j/k/l      " navigate between splits
Ctrl+w =            " equal size splits
Ctrl+w +            " increase split height
Ctrl+w -            " decrease split height
:q                   " close split/window
:only                " close all other splits

:tabnew file.txt    " open file in new tab
:tabn               " next tab
:tabp               " previous tab
gt                  " next tab (normal mode)
gT                  " previous tab (normal mode)
:tabo               " close all other tabs

File Operations

:w                  " save file
:w file.txt         " save as
:q                  " quit
:q!                 " quit without saving
:wq                 " save and quit
:x                  " save and quit (only if modified)
:e file.txt         " open file
:e!                 " reload file (discard changes)
:bn                 " next buffer
:bp                 " previous buffer
:ls                 " list buffers
:bd                 " close buffer

Marks & Jumps

ma                  " set mark 'a' in current line
'a                  " jump to mark 'a' (first col)
`a                  " jump to mark 'a' (exact position)
:marks              " list all marks

Ctrl+o              " jump to older position in jumplist
Ctrl+i              " jump to newer position in jumplist
:jumps              " show jumplist

Macros & Commands

qa                  " start recording macro 'a'
q                   " stop recording macro
@a                  " run macro 'a'
5@a                 " run macro 'a' 5 times
@@                  " repeat last macro

:!ls                " run shell command
:r !date            " insert command output into file
:sort               " sort lines in visual selection
:%!jq .             " filter file through jq (pretty print JSON)

Indentation & Formatting

>>                  " indent line right
<<                  " indent line left
5>>                 " indent 5 lines right
=                   " auto-indent (visual mode)
gg=G                " auto-indent entire file
:set tabstop=4      " tab display width
:set shiftwidth=4   " indent width
:set expandtab      " spaces instead of tabs
:retab              " convert tabs to spaces

Useful Tips

:set number         " show line numbers
:set relativenumber " show relative line numbers
:set hlsearch       " highlight all search matches
:set incsearch      " incremental search
:set ignorecase     " case-insensitive search
:set smartcase      " smart case sensitivity
:syntax on          " enable syntax highlighting
:set wrap           " line wrapping
:set mouse=a        " enable mouse support

gf                  " open file under cursor
Ctrl+a              " increment number under cursor
Ctrl+x              " decrement number under cursor
J                   " join line below to current
~                   " toggle case of character
gUU                 " uppercase entire line
guu                 " lowercase entire line

模式切换

i           " 在光标前进入插入模式
a           " 在光标后进入插入模式
I           " 在行首进入插入模式
A           " 在行尾进入插入模式
o           " 在下方新开一行并进入插入模式
O           " 在上方新开一行并进入插入模式
v           " 可视模式(字符选择)
V           " 可视模式(行选择)
Ctrl+v      " 可视模式(块选择)
Esc         " 返回普通模式

光标移动

h j k l             " 左、下、上、右
w                   " 下一个单词开头
b                   " 上一个单词开头
e                   " 单词结尾
0                   " 行首
^                   " 第一个非空白字符
$                   " 行尾
gg                  " 跳到第一行
G                   " 跳到最后一行
5G                  " 跳到第 5 行
%                   " 跳到匹配的括号
fx                  " 当前行下一个字符 x

剪切、复制与粘贴

dd                  " 删除(剪切)当前行
3dd                 " 删除 3 行
dw                  " 删除单词
d$                  " 删除到行尾
D                   " 删除到行尾(同 d$)
x                   " 删除光标处字符
X                   " 删除光标前字符

yy                  " 复制当前行
3yy                 " 复制 3 行
yw                  " 复制单词
y$                  " 复制到行尾

p                   " 在光标后粘贴
P                   " 在光标前粘贴

搜索与替换

/pattern            " 向下搜索
?pattern            " 向上搜索
n                   " 下一个匹配
N                   " 上一个匹配
*                   " 向下搜索光标下单词
#                   " 向上搜索光标下单词

:%s/old/new/g       " 全文替换
:%s/old/new/gc      " 全文替换(逐个确认)
:s/old/new/g        " 当前行替换
:5,10s/old/new/g    " 第 5-10 行替换
:noh                " 清除搜索高亮

撤销与重复

u                   " 撤销
Ctrl+r              " 重做
.                   " 重复上次修改

文本对象

diw                 " 删除光标所在单词
daw                 " 删除光标所在单词及周围空格
di"                 " 删除双引号内的内容
da"                 " 删除双引号及其内容
di(                 " 删除括号内的内容
da{                 " 删除花括号及其内容
ciw                 " 修改光标所在单词
ci"                 " 修改引号内的内容
yiw                 " 复制光标所在单词
vit                 " 选中 HTML 标签内的内容
vat                 " 选中 HTML 标签及其内容

窗口与标签页

:sp file.txt        " 水平分屏
:vsp file.txt       " 垂直分屏
Ctrl+w h/j/k/l      " 在分屏间移动
Ctrl+w =            " 等比分屏大小
Ctrl+w +            " 增加分屏高度
Ctrl+w -            " 减小分屏高度
:q                   " 关闭分屏/窗口
:only                " 关闭其他所有分屏

:tabnew file.txt    " 在新标签页打开文件
:tabn               " 下一个标签页
:tabp               " 上一个标签页
gt                  " 下一个标签页(普通模式)
gT                  " 上一个标签页(普通模式)
:tabo               " 关闭其他所有标签页

文件操作

:w                  " 保存文件
:w file.txt         " 另存为
:q                  " 退出
:q!                 " 不保存退出
:wq                 " 保存并退出
:x                  " 保存并退出(仅修改时保存)
:e file.txt         " 打开文件
:e!                 " 重新加载(丢弃修改)
:bn                 " 下一个缓冲区
:bp                 " 上一个缓冲区
:ls                 " 列出所有缓冲区
:bd                 " 关闭缓冲区

标记与跳转

ma                  " 在当前行设置标记 'a'
'a                  " 跳到标记 'a'(行首)
`a                  " 跳到标记 'a'(精确位置)
:marks              " 列出所有标记

Ctrl+o              " 跳转列表中后退
Ctrl+i              " 跳转列表中前进
:jumps              " 显示跳转列表

宏与命令

qa                  " 开始录制宏 'a'
q                   " 停止录制宏
@a                  " 运行宏 'a'
5@a                 " 运行宏 'a' 5 次
@@                  " 重复上次宏

:!ls                " 执行 shell 命令
:r !date            " 将命令输出插入文件
:sort               " 对可视选区排序
:%!jq .             " 通过 jq 格式化 JSON

缩进与格式化

>>                  " 当前行右缩进
<<                  " 当前行左缩进
5>>                 " 5 行右缩进
=                   " 自动缩进(可视模式)
gg=G                " 整个文件自动缩进
:set tabstop=4      " Tab 显示宽度
:set shiftwidth=4   " 缩进宽度
:set expandtab      " 用空格替代 Tab
:retab              " 将 Tab 转为空格

实用技巧

:set number         " 显示行号
:set relativenumber " 显示相对行号
:set hlsearch       " 高亮所有搜索匹配
:set incsearch      " 增量搜索
:set ignorecase     " 搜索忽略大小写
:set smartcase      " 智能大小写
:syntax on          " 启用语法高亮
:set wrap           " 自动换行
:set mouse=a        " 启用鼠标支持

gf                  " 打开光标下的文件
Ctrl+a              " 递增光标下数字
Ctrl+x              " 递减光标下数字
J                   " 将下一行合并到当前行
~                   " 切换光标处字符大小写
gUU                 " 当前行转大写
guu                 " 当前行转小写