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.
- Vim documentation (vimhelp.org)
- Vim source code (github.com)
Exiting
| Shortcut | Description |
|---|---|
:q | Close window |
:qa | Close all windows |
:w | Save |
:wq / :x | Save and close file |
ZZ | Save and quit |
:q! / ZQ | Quit without checking changes |
See: Writing and quitting
Exiting insert mode
| Shortcut | Description |
|---|---|
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
| Shortcut | Description |
|---|---|
a | Append |
A | Append from end of line |
i | Insert |
o | Next line |
O | Previous line |
s | Delete char and insert |
S | Delete line and insert |
C | Delete until end of line and insert |
r | Replace one character |
R | Enter Replace mode |
J | Join next line |
gJ | Join next line without space |
u | Undo changes |
<C-R> | Redo changes |
See: Insert mode
Clipboard
| Shortcut | Description |
|---|---|
x | Delete character |
dd | Delete line (Cut) |
yy | Yank line (Copy) |
p | Paste after cursor |
P | Paste before cursor |
"*p / "+p | Paste from system clipboard |
"*y / "+y | Copy to system clipboard |
See: Registers
Visual mode
| Shortcut | Description |
|---|---|
v | Enter visual mode |
V | Enter visual line mode |
<C-V> | Enter visual block mode |
gv | Reselect last selection |
In visual mode
| Shortcut | Description |
|---|---|
d / x | Delete selection |
s | Change selection |
y | Yank selection (Copy) |
> / < | Shift selection right/left |
o | Move to other end of selection |
See Operators for other things you can do.
Find and replace
| Shortcut | Description |
|---|---|
:%s/foo/bar/g | Replace in whole document |
:%s/foo/bar/gc | Replace with confirmation |
:s/foo/bar/g | Replace in current line |
See: Search and replace
Navigating
Directions
| Shortcut | Description |
|---|---|
h j k l | Arrow keys |
<C-U> / <C-D> | Half-page up/down |
<C-B> / <C-F> | Page up/down |
See: Left-right motions
Words
| Shortcut | Description |
|---|---|
b / w | Previous/next word |
ge / e | Previous/next end of word |
See: Word motions
Line
| Shortcut | Description |
|---|---|
0 (zero) | Start of line |
^ | Start of line (after whitespace) |
$ | End of line |
See: Line motions
Character
| Shortcut | Description |
|---|---|
fc | Go forward to character c |
Fc | Go backward to character c |
tc / Tc | Go just before/after c |
; / , | Repeat character search |
See: Character motions
Document
| Shortcut | Description |
|---|---|
gg | First line |
G | Last line |
:{number} | Go to line {number} |
{number}G | Go to line {number} |
{number}j | Go down {number} lines |
{number}k | Go up {number} lines |
See: Go to line
Screen
| Shortcut | Description |
|---|---|
zz | Center this line |
zt | Top this line |
zb | Bottom this line |
H | Move to top of screen |
M | Move to middle of screen |
L | Move to bottom of screen |
See: Scrolling
Search
| Shortcut | Description |
|---|---|
/pattern | Search forward |
?pattern | Search backward |
n / N | Next/previous match |
* / # | Next/previous word under cursor |
:noh | Clear 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.
| Operator | Motion |
|---|---|
d | w |
| Operator | Motion |
See: Operators
Operators list
| Shortcut | Description |
|---|---|
d | Delete |
y | Yank (copy) |
c | Change (delete then insert) |
> | Indent right |
< | Indent left |
= | Reindent |
g~ | Swap case |
gU | Uppercase |
gu | Lowercase |
! | Filter through external program |
See: Operators
Examples
Combine operators with motions to use them.
| Shortcut | Description |
|---|---|
dd | (repeat the letter) Delete current line |
dw | Delete to next word |
db | Delete to beginning of word |
2dd | Delete 2 lines |
dip | Delete a text object (inside paragraph) |
(in visual mode) d | Delete 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]round | Text object |
|---|---|---|
v | i | p |
| Operator | [i]nside or [a]round | Text object |
See: Text objects
Text objects
| Shortcut | Description |
|---|---|
p | Paragraph |
w | Word |
s | Sentence |
[ ( { < | A [], (), {}, or <> block |
' " ` | A quoted string |
b | Alias for ( block |
B | Alias for { block |
t | An XML tag block |
See: Text objects
Examples
| Shortcut | Description |
|---|---|
vip | Select paragraph |
vipipipip | Select more |
yip | Yank inner paragraph |
yap | Yank paragraph (including newline) |
dip | Delete inner paragraph |
cip | Change inner paragraph |
See Operators for other things you can do.
Files and windows
Diff
| Shortcut | Description |
|---|---|
vimdiff file1 file2 | Edit files in diff mode |
gvimdiff file1 file2 | Same, in the GUI |
:diffthis | Add window to diff mode |
:diffoff | Stop diff mode |
:set diffopt+=linematch:60 | Align similar diff lines |
See: Diff mode
Tab pages
| Shortcut | Description |
|---|---|
:tabnew / :tabedit | New tab |
:tabfind [file] | Find file in a new tab |
:tabclose | Close current tab |
:tabs | List all tabs |
gt / gT | Next/previous tab |
:tabfirst | Go to first tab |
:tablast | Go to last tab |
:tabn / :tabp | Next/previous tab |
Vim 9.2 adds a vertical tabpanel: :set showtabpanel=2 always shows it.
See: Tab pages
Windows
| Shortcut | Description |
|---|---|
:sp [file] | Split horizontally |
:vsp [file] | Split vertically |
<C-W>w | Next window |
<C-W>h/j/k/l | Go to window left/down/up/right |
<C-W>c | Close window |
<C-W>o | Close other windows |
z{height}<Cr> | Resize to {height} lines |
:terminal | Open a terminal window |
:vertical terminal | Terminal in vertical split |
Reference
Folds
| Shortcut | Description |
|---|---|
zo / zO | Open |
zc / zC | Close |
za / zA | Toggle |
zv | Open folds for this line |
zM | Close all |
zR | Open all |
zm | Fold more (foldlevel += 1) |
zr | Fold less (foldlevel -= 1) |
zi | Toggle folding |
zx | Update folds |
Uppercase ones are recursive (eg, zO is open recursively).
See: Folds
Navigation
| Shortcut | Description |
|---|---|
% | Matching (), [], {} |
[( [{ | Previous unmatched (/{ |
]) ]} | Next unmatched )/} |
[m / [M | Previous method start/end |
]m / ]M | Next method start/end |
See: Matching pairs
Jumping
| Shortcut | Description |
|---|---|
<C-O> | Go back to previous location |
<C-I> | Go forward |
gf | Go to file in cursor |
See: Jump motions
Counters
| Shortcut | Description |
|---|---|
<C-A> | Increment number |
<C-X> | Decrement |
g<C-A> / g<C-X> | In visual mode: increment/decrement lines |
See: CTRL-A
Tags
| Shortcut | Description |
|---|---|
:tag Classname | Jump 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 Classname | Find definitions of Classname |
:tjump Classname | Find definitions of Classname (jump if one) |
See: Tags
Case
| Shortcut | Description |
|---|---|
~ | Toggle case (Case => cASE) |
gU | Uppercase |
gu | Lowercase |
gUU | Uppercase current line (also gUgU) |
guu | Lowercase current line (also gugu) |
Do these in visual or normal mode.
See: Changing case
Marks
| Shortcut | Description |
|---|---|
`^ | Last position of cursor in insert mode |
`. | Last change in current buffer |
`" | Last exited current buffer |
`0 | In 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 |
ma | Mark this cursor position as a |
`a | Jump to the cursor position a |
'a | Jump to the beginning of the line with position a |
d'a | Delete from current line to line of mark a |
d`a | Delete from current position to position of mark a |
c'a | Change text from current line to line of a |
y`a | Yank text from current position to position of a |
:marks | List all current marks |
:delm a | Delete mark a |
:delm a-d | Delete marks a, b, c, d |
:delm abc | Delete marks a, b, c |
See: Marks
Misc
Misc
| Shortcut | Description |
|---|---|
. | Repeat last command |
]p | Paste under the current indentation level |
:set ff=unix | Write with Unix line endings |
:set ff=dos | Write with DOS line endings |
See: Repeating commands
Command line
| Shortcut | Description |
|---|---|
<C-R><C-W> | Insert current word |
<C-R>" | Paste from unnamed register |
See: Command-line editing
Insert completion
| Shortcut | Description |
|---|---|
<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>s | Suggest spelling |
Vim 9.2 matches fuzzily with :set completeopt+=fuzzy.
See: Insert completion
Text alignment
:center [width]
:right [width]
:left
See: Aligning text
Calculator
| Shortcut | Description |
|---|---|
<C-R>=128/2 | Shows 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
| Shortcut | Description |
|---|---|
:set spell spelllang=en_us | Turn on US English spell checking |
]s | Next misspelled word |
[s | Previous misspelled word |
z= | Suggest spellings |
zg | Add word to spell list |
zw | Mark word as misspelled |
zug | Undo zg/zw for word |
<C-X>s | Suggest spelling (insert mode) |
See: Spell checking
Also see
- Vim 9.2 release notes (vim.org)
- Vim FAQ (vimhelp.org)
- Vim downloads (vim.org)
0 Comments for this cheatsheet. Write yours!