Skip to content

Vim Commands Cheat Sheet

A concise reference for the most common and essential Vim commands, from basic editing to advanced operations.

Exiting

CommandDescription
:wWrite (save) the current file.
:waWrite all open files.
:qQuit the current file.
:qaQuit all open files.
:q! / ZQQuit and discard any unsaved changes.
:wq / :xWrite (save) and quit.
ZZA shortcut for :wq.
:wqaWrite and quit all open files.

Modes

CommandDescription
iInsert mode (before cursor).
aAppend mode (after cursor).
o / OOpen a new line below/above and enter Insert mode.
vVisual mode (character-wise).
VVisual mode (line-wise).
<C-v>Visual mode (block-wise).
:Command-Line mode.
RReplace mode.
EscReturn to Normal mode.

Basic Movement

CommandDescription
h j k lMove left, down, up, right.
w / bMove forward/backward by one word.
eMove to the end of the current word.
0 / ^Move to the start / first non-blank char of the line.
$ / g_Move to the end / last non-blank char of the line.
gg / GGo to the first / last line of the file.
{n}GGo to line number {n}.
H / M / LGo to the High, Middle, or Low part of the screen.

Editing Text

Deleting, Changing, and Replacing

CommandDescription
xDelete character under the cursor.
d{motion}Delete text over {motion}.
ddDelete the current line.
DDelete from cursor to the end of the line.
c{motion}Change text over {motion}.
ccChange the entire line.
CChange to the end of the line.
r{char}Replace the character under the cursor with {char}.
sSubstitute character and enter Insert mode.
.Repeat the last change.
u / <C-r>Undo / Redo the last change.

Yank (Copy) and Put (Paste)

The y command yanks (copies) text, and p puts (pastes) it.

CommandDescription
y{motion}Yank text over {motion}.
yyYank the current line.
YYank the current line (same as yy).
p / PPut (paste) after / before the cursor.

Changing Case

CommandDescription
~Switch case of the character under the cursor.
gU{motion}Convert text over {motion} to UPPERCASE.
gu{motion}Convert text over {motion} to lowercase.
gUU / guuMake the entire line UPPERCASE / lowercase.

Search and Replace

Searching

CommandDescription
/patternSearch forward for pattern.
?patternSearch backward for pattern.
* / #Search forward/backward for the word under the cursor.
n / NGo to the next / previous search match.

Replacing

CommandDescription
:s/old/new/gReplace all old with new on the current line.
:%s/old/new/gReplace all old with new in the entire file.
:%s/old/new/gcReplace throughout the file, with confirmation for each.

Working with Multiple Files (Buffers)

CommandDescription
:e {file}Edit a file in a new buffer.
:lsList all open buffers.
:b{n}Go to buffer number {n}.
:bn / :bpGo to the next / previous buffer.
:bdBuffer delete (close the current buffer).

Window Management

CommandDescription
:sp {file}Split the window horizontally.
:vsp {file}Vertically split the window.
<C-w>sHorizontal split.
<C-w>vVertical split.
<C-w>wSwitch to the next window.
<C-w>h/j/k/lMove to the window left/down/up/right.
<C-w>qQuit (close) the current window.
<C-w>=Make all windows equal size.

Marks and Jumps

Marks are like bookmarks. Jumps are cursor movements that are saved in a list.

CommandDescription
m{a-z}Set a mark named {a-z} at the cursor position.
{a-z}Jump to the line of mark {a-z}.
'aJump to the beginning of the line of mark a.
:marksList all marks.
<C-o> / <C-i>Jump to the older / newer position in the jump list.

Macros

Macros record a sequence of keystrokes that you can replay.

  1. q{a-z} - Start recording a macro into register {a-z}.
  2. Perform your sequence of commands.
  3. q - Stop recording.
  4. @{a-z} - Execute the macro from register {a-z}.
  5. @@ - Replay the last executed macro.
最近更新