Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 36 ยท Getting Started

The Vim Grammar: Operators, Counts and Motions

Understand how Vim commands combine an optional count, an operator and a motion or text object, so a few keys unlock hundreds of commands. Includes a parser.

Vim feels like it has thousands of commands, but most of them are combinations of a small vocabulary. Learning that grammar is the best investment you can make in the first week, because it turns memorizing into understanding. This lesson introduces the pattern and then builds a parser that splits any command into its parts.

The sentence

A typical Normal-mode command has this shape:

TEXT
[count] operator [count] motion-or-object
  • The operator is a verb: d delete, c change, y yank (copy), > indent, < unindent, gU uppercase, gu lowercase, = auto-indent.
  • The motion is where the verb stops: w word, $ end of line, } next paragraph, G end of file.
  • A text object is a structured noun such as iw (inner word) or i" (inside quotes). They are covered in chapter 3.
  • A count repeats: 3dw and d3w both delete three words.

So dw means "delete to the next word start", d$ means "delete to the end of the line", and y} means "copy up to the end of the paragraph". If you learn five operators and ten motions you know fifty commands.

Operators are verbs

d Delete
c Change
y Yank (copy)
Indent

An operator waits for a motion.

1 / 2

Doubling an operator

Typing an operator twice means "the whole line": dd deletes the line, yy yanks it, cc changes it, >> indents it, gUU uppercases it. The rule is consistent: dd works as if you had typed d followed by "line".

A count with a doubled operator affects several lines: 3dd deletes three lines, 5>> indents five.

Motions without operators

Every motion also works alone, simply moving the cursor. That is why learning motions pays twice: w moves, and dw deletes by the same distance. A motion is either exclusive (stops before the target character) or inclusive (includes it); e is inclusive while w is exclusive. That detail explains why dw and de delete slightly different amounts. :help exclusive describes the rules.

Case study: change a word

Suppose the cursor sits in the word colour and you want a different word. Three equivalent ideas:

  1. cw changes to the end of the word and enters Insert mode.
  2. ciw changes the whole word wherever the cursor is inside it.
  3. bcw first moves to the word start, then changes.

The grammar says c + iw is more reliable, because the object includes the whole word, not just the part from the cursor.

A grammar parser (stand-in)

The block below parses a Normal-mode command into count, operator, and target. It is a stand-in for Vim's own parser, made small to show the idea.

JS Command parser (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice how 2d3w would multiply to six words. Vim does the same: counts before and after the operator multiply.

Reading commands aloud

A good way to learn is to read each command as English:

Keys Reads as
dap delete a paragraph
ci( change inside parentheses
yi" yank inside quotes
>ip indent inner paragraph
gUiw uppercase inner word
d/foo<Enter> delete up to the next "foo"

Search is a motion too, so d/foo deletes from the cursor up to (not including) the next match. Chapter 4 returns to this.

Repeating and undoing

Two keys make the grammar practical:

  • . repeats the last change, including its operator, motion and any typed text.
  • u undoes one change, which is usually one whole command (an entire Insert session counts as one change).

Because the whole sentence is the unit, . redoes ciw plus the word you typed. That is why "change inner word, then move and press ." is such a common rhythm. Lesson 3 of chapter 3 covers it fully.

Mapping the grammar to a keyboard

Here is a second stand-in that applies an operator to a line using a motion width, to see the result of exclusive and inclusive behavior.

JS Exclusive vs inclusive (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

dw removes the word and its trailing space, while de leaves the space behind. Knowing that difference prevents tiny mistakes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does d3w do?

    The operator d plus a counted motion 3w deletes from the cursor to the start of the word three words ahead.
  2. 2. How do you apply an operator to the current line?

    A doubled operator (dd, yy, cc, >>) works on the whole line.
  3. 3. What is the difference between w and e?

    dw stops before the next word, while de includes the last character of the current word.
  4. 4. What does 2d3w delete?

    Counts multiply, so 2d3w deletes 2 x 3 = 6 words.

Summary

  • Commands read as [count] operator [count] motion-or-object.
  • Operators are verbs (d c y > < gU); motions and text objects are what they act on.
  • Doubling an operator means the whole line; counts multiply.
  • Every motion works alone to move the cursor.
  • . repeats the whole last change, which makes the grammar powerful.

Next: Modes: Normal, Insert and Command-line