← 输入框|devhints.io|刷新

Getting started

Introduction

Vim is a highly efficient text editor. This reference covers Vim 9.2.

For shortcut notation, see :help key-notation. Run :Tutor for the built-in tutorial.

Exiting

ShortcutDescription
:qClose window
:qaClose all windows
:wSave
:wq / :xSave and close file
ZZSave and quit
:q! / ZQQuit without checking changes

See: Writing and quitting

Exiting insert mode

ShortcutDescription
Esc / <C-[>Exit insert mode
<C-C>Exit insert mode without abbreviations
<C-O>Run one command, then resume insert

See: Insert mode

Editing

Editing

ShortcutDescription
aAppend
AAppend from end of line
iInsert
oNext line
OPrevious line
sDelete char and insert
SDelete line and insert
CDelete until end of line and insert
rReplace one character
REnter Replace mode
JJoin next line
gJJoin next line without space
uUndo changes
<C-R>Redo changes

See: Insert mode

Clipboard

ShortcutDescription
xDelete character
ddDelete line (Cut)
yyYank line (Copy)
pPaste after cursor
PPaste before cursor
"*p / "+pPaste from system clipboard
"*y / "+yCopy to system clipboard

See: Registers

Visual mode

ShortcutDescription
vEnter visual mode
VEnter visual line mode
<C-V>Enter visual block mode
gvReselect last selection

In visual mode

ShortcutDescription
d / xDelete selection
sChange selection
yYank selection (Copy)
> / <Shift selection right/left
oMove to other end of selection

See Operators for other things you can do.

Find and replace

ShortcutDescription
:%s/foo/bar/gReplace in whole document
:%s/foo/bar/gcReplace with confirmation
:s/foo/bar/gReplace in current line

See: Search and replace

Directions

ShortcutDescription
h j k lArrow keys
<C-U> / <C-D>Half-page up/down
<C-B> / <C-F>Page up/down

See: Left-right motions

Words

ShortcutDescription
b / wPrevious/next word
ge / ePrevious/next end of word

See: Word motions

Line

ShortcutDescription
0 (zero)Start of line
^Start of line (after whitespace)
$End of line

See: Line motions

Character

ShortcutDescription
fcGo forward to character c
FcGo backward to character c
tc / TcGo just before/after c
; / ,Repeat character search

See: Character motions

Document

ShortcutDescription
ggFirst line
GLast line
:{number}Go to line {number}
{number}GGo to line {number}
{number}jGo down {number} lines
{number}kGo up {number} lines

See: Go to line

Screen

ShortcutDescription
zzCenter this line
ztTop this line
zbBottom this line
HMove to top of screen
MMove to middle of screen
LMove to bottom of screen

See: Scrolling

ShortcutDescription
/patternSearch forward
?patternSearch backward
n / NNext/previous match
* / #Next/previous word under cursor
:nohClear search highlight

See: Search commands

Operators

Usage

Operators let you operate in a range of text (defined by motion). These are performed in normal mode.

OperatorMotion
dw
OperatorMotion

See: Operators

Operators list

ShortcutDescription
dDelete
yYank (copy)
cChange (delete then insert)
>Indent right
<Indent left
=Reindent
g~Swap case
gUUppercase
guLowercase
!Filter through external program

See: Operators

Examples

Combine operators with motions to use them.

ShortcutDescription
dd(repeat the letter) Delete current line
dwDelete to next word
dbDelete to beginning of word
2ddDelete 2 lines
dipDelete a text object (inside paragraph)
(in visual mode) dDelete selection

See: :help motion.txt

Text objects

Usage

Text objects let you operate (with an operator) in or around text blocks (objects).

Operator[i]nside or [a]roundText object
vip
Operator[i]nside or [a]roundText object

See: Text objects

Text objects

ShortcutDescription
pParagraph
wWord
sSentence
[ ( { <A [], (), {}, or <> block
' " `A quoted string
bAlias for ( block
BAlias for { block
tAn XML tag block

See: Text objects

Examples

ShortcutDescription
vipSelect paragraph
vipipipipSelect more
yipYank inner paragraph
yapYank paragraph (including newline)
dipDelete inner paragraph
cipChange inner paragraph

See Operators for other things you can do.

Files and windows

Diff

ShortcutDescription
vimdiff file1 file2Edit files in diff mode
gvimdiff file1 file2Same, in the GUI
:diffthisAdd window to diff mode
:diffoffStop diff mode
:set diffopt+=linematch:60Align similar diff lines

See: Diff mode

Tab pages

ShortcutDescription
:tabnew / :tabeditNew tab
:tabfind [file]Find file in a new tab
:tabcloseClose current tab
:tabsList all tabs
gt / gTNext/previous tab
:tabfirstGo to first tab
:tablastGo to last tab
:tabn / :tabpNext/previous tab

Vim 9.2 adds a vertical tabpanel: :set showtabpanel=2 always shows it.

See: Tab pages

Windows

ShortcutDescription
:sp [file]Split horizontally
:vsp [file]Split vertically
<C-W>wNext window
<C-W>h/j/k/lGo to window left/down/up/right
<C-W>cClose window
<C-W>oClose other windows
z{height}<Cr>Resize to {height} lines
:terminalOpen a terminal window
:vertical terminalTerminal in vertical split

See: Windows, Terminal

Reference

Folds

ShortcutDescription
zo / zOOpen
zc / zCClose
za / zAToggle
zvOpen folds for this line
zMClose all
zROpen all
zmFold more (foldlevel += 1)
zrFold less (foldlevel -= 1)
ziToggle folding
zxUpdate folds

Uppercase ones are recursive (eg, zO is open recursively).

See: Folds

ShortcutDescription
%Matching (), [], {}
[( [{Previous unmatched (/{
]) ]}Next unmatched )/}
[m / [MPrevious method start/end
]m / ]MNext method start/end

See: Matching pairs

Jumping

ShortcutDescription
<C-O>Go back to previous location
<C-I>Go forward
gfGo to file in cursor

See: Jump motions

Counters

ShortcutDescription
<C-A>Increment number
<C-X>Decrement
g<C-A> / g<C-X>In visual mode: increment/decrement lines

See: CTRL-A

Tags

ShortcutDescription
:tag ClassnameJump to first definition of Classname
<C-]>Jump to definition
g]See all definitions
<C-T>Go back to last tag
<C-O> <C-I>Back/forward
:tselect ClassnameFind definitions of Classname
:tjump ClassnameFind definitions of Classname (jump if one)

See: Tags

Case

ShortcutDescription
~Toggle case (Case => cASE)
gUUppercase
guLowercase
gUUUppercase current line (also gUgU)
guuLowercase current line (also gugu)

Do these in visual or normal mode.

See: Changing case

Marks

ShortcutDescription
`^Last position of cursor in insert mode
`.Last change in current buffer
`"Last exited current buffer
`0In last file edited
''Back to line in current buffer where jumped from
``Back to position in current buffer where jumped from
`[To beginning of previously changed or yanked text
`]To end of previously changed or yanked text
`<To beginning of last visual selection
`>To end of last visual selection
maMark this cursor position as a
`aJump to the cursor position a
'aJump to the beginning of the line with position a
d'aDelete from current line to line of mark a
d`aDelete from current position to position of mark a
c'aChange text from current line to line of a
y`aYank text from current position to position of a
:marksList all current marks
:delm aDelete mark a
:delm a-dDelete marks a, b, c, d
:delm abcDelete marks a, b, c

See: Marks

Misc

Misc

ShortcutDescription
.Repeat last command
]pPaste under the current indentation level
:set ff=unixWrite with Unix line endings
:set ff=dosWrite with DOS line endings

See: Repeating commands

Command line

ShortcutDescription
<C-R><C-W>Insert current word
<C-R>"Paste from unnamed register

See: Command-line editing

Insert completion

ShortcutDescription
<C-N> / <C-P>Complete word from buffer
<C-X><C-F>Complete file path
<C-X><C-L>Complete whole line
<C-X><C-R>Complete from registers (Vim 9.2)
<C-X>sSuggest spelling

Vim 9.2 matches fuzzily with :set completeopt+=fuzzy.

See: Insert completion

Text alignment

:center [width]
:right [width]
:left

See: Aligning text

Calculator

ShortcutDescription
<C-R>=128/2Shows the result of the division : '64'

Do this in insert mode.

See: Expression register

Exiting with an error

:cq
:cquit

Quits like :qa!, abandoning changes, and returns an error code. Great for aborting Git commands.

See: :cquit

Spell checking

ShortcutDescription
:set spell spelllang=en_usTurn on US English spell checking
]sNext misspelled word
[sPrevious misspelled word
z=Suggest spellings
zgAdd word to spell list
zwMark word as misspelled
zugUndo zg/zw for word
<C-X>sSuggest spelling (insert mode)

See: Spell checking

Also see

0 Comments for this cheatsheet. Write yours!