The Universal Grammar

An optional count and register, an operator, an optional count, then a motion or text object

Keys: d, c, y, >, <, =, gU, gu, g~, w, iw, ip, $, %

Vim is a tiny language: verbs (operators) compose with nouns (motions and text objects). Once you see the grammar, every key falls into place.

Almost every command in Vim follows one shape:

[count] ["reg] operator [count] motion-or-text-object

Read it as a sentence: do this thing, optionally with a register, this many times (omitted counts default to 1), to this stretch of text. (Registers are named clipboards โ€” the "a prefix means register a; the full treatment is in Registers.) Concrete examples:

Command Reads as
dw Delete one word forward
3dw Three times, delete one word (same result as below)
d3w Once, delete three words (same result as above; the counts multiply)
ciw Change the inner word the cursor is on
y$ Yank from cursor to end of line
>ip Indent inner paragraph
"ayy Yank this line into register a
gUiw Uppercase the inner word

The verbs (operators)

Operator Meaning
d Delete (cut)
c Change (delete then enter insert mode)
y Yank (copy)
> Indent right
< Indent left
= Auto-indent / format
gU Uppercase
gu Lowercase
g~ Toggle case
gq Format (line-wrap)
! Filter through external command

The nouns (motions and text objects)

Anything that moves the cursor in normal mode is a motion. After an operator, the same motion describes the range the operator acts on. So w alone moves the cursor a word forward; dw deletes the same span.

A word is a run of letters, digits, and underscores โ€” any punctuation (,, ., !, -, etc.) breaks it. A WORD (capitalized) is anything separated by whitespace, so it may contain punctuation and symbols. For example, foo-bar.baz is one WORD but three words.

Key Meaning
w Forward to the start of the next word
b Backward to the start of the previous word
e Forward to the end of this word (or the next, if already at the end)
W Like w, but using WORDs
B Like b, but using WORDs
E Like e, but using WORDs

Move horizontally within the current line:

Key Meaning
0 Beginning of the line (column zero)
^ First non-blank character of the line
$ End of the line
_ Like ^, but accepts a count: 3_ is the first non-blank three lines down
g_ Last non-blank character of the line

Move within the whole file:

Key Meaning
gg Top of the file
G Bottom of the file
{n}G Jump to line number {n} (e.g. 42G for line 42)

Hop to a specific character without leaving the line:

Key Meaning
f{c} Forward to character {c}
F{c} Backward to character {c}
t{c} Forward to just before {c} (the t is for till)
T{c} Backward to just after {c}
; Repeat the last f / F / t / T in the same direction
, Repeat it in the opposite direction

Pattern-based motion โ€” jump to whatever matches:

Key Meaning
/{pattern} Search forward for {pattern}
?{pattern} Search backward for {pattern}
n Repeat the last search in its original direction
N Repeat the last search in the opposite direction
* Search forward for the word under the cursor
# Search backward for the word under the cursor

Move by larger units of text:

Key Meaning
( Beginning of the previous sentence
) Beginning of the next sentence
{ Beginning of the previous paragraph
} Beginning of the next paragraph

Jump between matched pairs:

Key Meaning
% Jump to the matching (, [, {, /* */, #if / #endif, etc.

Text objects describe a region by its structure rather than its endpoints โ€” the word the cursor is in, the parens the cursor is between. The prefix decides what's included: i for the inner contents (no delimiters), a for a whole object (including its delimiters and trailing whitespace). They only work after an operator or in visual mode โ€” they're nouns, not motions.

Key Meaning
iw The word the cursor is on
aw A word plus its trailing whitespace
i" Inside a double-quoted string (excluding the quotes)
a( A parenthesized group, including the parens
ip Inside a paragraph
at An HTML/XML tag, including both opening and closing tags

The full treatment of text objects is in Text Objects โ€” for now, treat them as nouns like any other.

The Vim Grammar in Action

Worked example โ€” operator + motion = sentence

Two operator+motion sentences worked through.

Step 1 ยท Cursor on q.
[missing screenshot foundations.grammar/frame_01.color.svg โ€” run python content/render_screenshots.py]

Starting in normal mode. We'll run dw: the delete operator followed by the word motion.

Step 2 ยท dw ยท dw โ€” quick deleted.
[missing screenshot foundations.grammar/frame_02.color.svg โ€” run python content/render_screenshots.py]

Operator d (verb) plus motion w (noun). The same grammar applies to any operator+motion pair.

Step 3 ยท gUw ยท gUw โ€” BROWN uppercased.
[missing screenshot foundations.grammar/frame_03.color.svg โ€” run python content/render_screenshots.py]

Same noun, new verb: keep the motion w (a word forward), but swap the operator for gU (uppercase). The grammar lets us compose any operator with any motion โ€” no new syntax to learn.

See also: The Vim Grammar, Delete, Change, Yank and Put, Inner Word vs Around Word