← 全部文章

Vim Keystrokes

An everyday Vim reference covering movement, editing, search, substitution, files, and help.

  • #Vim
  • #编辑器
本文目录
  1. Modes & notation
  2. Move & jump
  3. Operator + count + motion
  4. Insert, delete & change
  5. Select, copy & put
  6. Undo, redo & repeat
  7. Search & options
  8. Substitute
  9. Files & external commands
  10. Help

A compact reference for everyday editing, using Vim’s default behavior.

Modes & notation

Commands start in Normal mode unless noted. <Esc> returns from Insert, Replace, or Visual mode to Normal mode. : opens the command line.

  • <C-r> means Ctrl + r; <Enter> executes :, /, and ? commands.
  • [count] is optional; {char}, {file}, and {text} are placeholders.
  • Type sequences directly: ddp means dd then p. Arrows between steps are explanatory.

Move & jump

KeysAction
h / j / k / lLeft / down / up / right
w / WNext word / WORD start
b / BPrevious word / WORD start, or start of the current one
e / ENext word / WORD end (current one if before its end)
0 / ^ / $Line start / first non-blank / line end
f{char} / F{char}Find character forward / backward on this line
t{char} / T{char}Move toward character forward / backward; stop one character short
; / ,Repeat last f/F/t/T in the same / opposite direction
%Jump to the matching bracket: (), [], {}
gg / GFirst / last line of the file
30GLine 30
<C-o> / <C-i>Older / newer position in the jump list
<C-g>Show file status and cursor position

word vs WORD: a word separates keyword characters from punctuation; a WORD is separated by whitespace. With default keyword rules, foo-bar is three words but one WORD. See :help word for details.

% can also find a bracket at or after the cursor on the current line. Combine movements: j$ goes to the end of the next line.

Operator + count + motion

[count]operator[count]motion — d deletes, c changes, y yanks (copies).

ExampleAction
d2w / 2dwDelete over two w movements
2d3wDelete over six w movements; counts multiply
d$ / c$ / y$Delete / change / yank to line end
3ddDelete three lines, starting with this one

Text objects select a region instead of moving to a destination:

ObjectMeaningExamples
iwInner word, without surrounding whitespacediw, ciw, yiw
awA word, including adjacent whitespacedaw, caw, yaw

Use diw to delete a whole word from inside it; dw starts at the cursor.

Insert, delete & change

KeysAction
i / aInsert before / after the cursor
I / AInsert before the first non-blank / at line end
o / OOpen a line below / above and enter Insert mode
x / XDelete the character under / before the cursor
dwDelete up to the next word start, excluding that start¹
deDelete through the next word end, including that end
d$ / DDelete to line end
ddDelete the whole line
ceDelete through the next word end, then enter Insert mode
c$ / CChange to line end
ccChange the whole line
r{char}Replace the character under the cursor; stay in Normal mode
R{text}<Esc>Replace successive characters, then return to Normal mode

All c commands enter Insert mode. On a non-blank character, cw behaves like ce, leaving following whitespace alone.

¹ When the last word crossed by dw is at line end, deletion stops there. de / ce reach the current word end if before it, or the next word end if already on it.

Select, copy & put

KeysAction
v / V / <C-v>Start character / line / block selection
y / d / c in Visual modeYank / delete / change the selection
ywYank up to the next word start; same line-end rule as dw
yiwYank the whole word under the cursor
yyYank the whole line
p / PPut characterwise text after / before the cursor
p / P with whole linesPut below / above the current line

p/P put yanked or deleted text from a register (Vim’s text storage).

  • Copy a selection: v → move to select → y → move to destination → p.
  • V4jy: select this line plus four below, then yank all five.
  • ddp: move this line below the next one (when a next line exists).

Undo, redo & repeat

KeysAction
uUndo the last change
UUndo the latest changes on the last-modified line; itself undoable
<C-r>Redo an undone change
.Repeat the last change

Search & options

KeysAction
/pattern / ?patternSearch forward / backward; press <Enter>
n / NRepeat the search in the same / opposite direction
* / #Search forward / backward for the whole word under the cursor
/pattern\c / /pattern\CIgnore / match case for this pattern, including repeats

Search patterns are regular expressions, not necessarily literal text.

Option or commandShort formEffect
:set ignorecase:set icIgnore case
:set noignorecase:set noicMatch case
:set hlsearch:set hlsHighlight search matches
:nohlsearch:nohClear highlights until the next search
:set nohlsearch:set nohlsDisable search highlighting
:set incsearch:set isPreview matches while typing

Combine options: :set hls is. Pattern flags \c/\C override case options.

Substitute

:[range]s/pattern/replacement/[flags] — default range: current line.

CommandAction
:s/old/new/Replace the first match on this line
:s/old/new/gReplace every match on this line
:10,20s/old/new/gReplace every match on lines 10–20, inclusive
:%s/old/new/gReplace every match in the file
:%s/old/new/gcReplace across the file, confirming each match

% means all lines; g means all matches per line; c asks for confirmation.

Confirmation keyAction
y / nReplace / skip this match
aReplace this and all remaining matches
q / <Esc>Stop replacing
lReplace this match, then stop
<C-e> / <C-y>Scroll text up / down to inspect below / above

Files & external commands

CommandAction
:e {file}Open a file
:wSave the current file
:w {file}Write to another file; keep the current name if already named
:saveas {file}Save under a new name and use that name
:qQuit the current window; normally refuses unsaved changes
:wqSave and quit the current window
:q!Force quit the current window; discard changes if the buffer is closed
:r {file}Insert a file’s contents below the current line
:!lsRun the external shell command ls
:r !lsInsert the command’s output below the current line

Help

CommandLook up
:help wNormal-mode command w
:help :wCommand-line command :w
:help 'ignorecase'An option

In help: <C-]> follows a help tag, <C-o> goes back, and :q closes the help window.

全文完 回到顶部 ↑