Lesson 3 — Operators, Counts and the Grammar

You typed a long command and one word in the middle is wrong. You can hold backspace and watch fourteen correct characters disappear along with the bad one — or you can say what you actually mean: change this word. That sentence is three keys. And once you can say it, you can say a hundred other things, because Vim is not a list of shortcuts to memorise. It is a small grammar you compose.

This lesson is where the vocabulary from Lesson 2 starts paying compound interest.

The sentence: count, operator, motion

Every command reads the same way:

(count) (operator) (motion or text object)

  • count — how many times. Optional; one if you leave it out.
  • operator — what to do: d delete, c change, y yank.
  • motion — how far. Every motion you learned in Lesson 2, or a text object (Lesson 4).

d means delete. w means to the start of the next word. So dw deletes to the start of the next word. You did not learn dw — you learned d, and you already knew w. The same trade gives you db, de, dW, d$ and dt/ at no extra cost.

The count can sit in front of the operator or in front of the motion. 3dw and d3w both delete three words. Use whichever you think of first.

The three operators

OperatorSaysWhere it works
ddeleteBoth
cchange — delete, then leave you typingBoth
yyank, which is Vim's word for copy (Lesson 5)Both
> <indent / outdentCode editors

Where this works. In a code editor an operator reaches anywhere in the file. In a terminal it reaches exactly one line — the one you are typing — and only while nothing is running; output further up has already happened, so nothing there can be changed. Motions, selection and y work everywhere, always, in both halves.

Commands that read as one sentence each

PressReads asWhat you get
dwdelete wordremoves the word ahead and the space after it
dedelete to end of wordremoves the word, leaves the space
d$delete to end of lineclears everything from the cursor rightwards
3dwdelete three wordsone keystroke more than dw, three times the work
dt/delete till slashstops just before the next / on the line

Swap the operator and the sentence still parses: cw ce c$ change instead of delete, yw ye y$ copy instead of delete. That is the whole trick — three operators times a dozen motions is a lot of commands you never had to learn.

c is worth a moment on its own. It deletes the span and drops you straight into typing, exactly where the old text was — so after cw you are typing in the middle of your command, not back at the end of it.

Doubling the operator takes the whole line

Type the operator twice and it applies to the line itself.

PressDoesWhere it works
dddelete the whole line — in a terminal see the note belowBoth
ccclear the line and start typingBoth
yycopy the whole lineBoth
3yywith a count, that many lines — a yank may cross rowsBoth
2ddwith a count, that many linesCode editors. In a terminal dd clears the line you are typing, whatever count you give it

In a terminal, dd is the fastest way to abandon a half-typed command without sending it: Esc then dd.

How much it clears depends on who wrapped the line. When the terminal wrapped it — which is what a shell does with anything too long — the whole command goes, however many rows it covered. A program that draws its own input box breaks the lines itself, and a CLI coding agent's box is one: there dd clears the row you are on, so a three-row prompt takes three of them.

Single-key edits

Some errands are too small to deserve a motion, so they get their own key.

PressDoes
xdelete the character under the cursor — 3x for three
Xdelete the character before it
rreplace one character: r then the new one, and you stay in normal mode
ssubstitute — delete the character and start typing
Ddelete to the end of the line
Cchange to the end of the line

Where this works: both halves. In a terminal all six reach only the line you are typing, like every other edit.

Changing case

Case is an operator too, which means it composes exactly like d and c do.

PressDoesWhere it works
~flip the case of the character under the cursor — 5~ for fiveBoth
guiwlowercase the word you are onBoth
gUiwuppercase the word you are onBoth
g~eflip case from here to the end of the wordBoth
guu gUUlowercase / uppercase the whole lineBoth
u U ~ in visual modelowercase / uppercase / flip the selectionBoth

Changing case changes text, so in a terminal all of these reach only the line you are typing, like every other edit.

Note the one collision worth knowing: u in visual mode lowercases the selection. u in normal mode is undo in a code editor, and does nothing at all in a terminal — see below.

. repeats the last change

The dot repeats whatever you just did. daw then . . removes three words. 3x then . removes three more characters. Counts and visual selections are part of what gets remembered, so vwd repeats too.

Two things about the dot in a terminal:

  • 3. does it three times. Vim's own rule substitutes the count into the original change, so there 3x then 2. means 2x. Here it means do that again, three times. The two agree whenever the original change carried no count of its own, which is nearly always.
  • A change that ended in typing is not remembered. After ciw, pressing . does nothing — retype the change instead. The dot repeats deletions.

In a code editor, . behaves the way it does in Vim.

Undo, join and the command line — code editors only

PressDoes
uundo
CtrlRredo
Jjoin this line and the next
:42jump to line 42
:%s/old/new/gsubstitute across the file
:sortsort the lines
:nohclear the search highlight

:g and :v, :reg, :set and the :map family are there too. What is not there is the file commands — :w, :q, :wq and :e do nothing. Files save themselves; S / CtrlS forces a save immediately, and you close a pane the way you close any other tab.

What the terminal half will not do

Two sentences will carry you until Lesson 6.

There is no : and no u in a terminal, and edits reach only the line you are typing, and only while nothing is running. Motions, selection, search and yank are exempt: they work over the whole screen, all the time.

Lesson 6 — Terminals and CLI Coding Agents covers it properly.

Try it

In a terminal with Vim keys on, type a command but do not press Enter:

  1. Type echo alpha beta gamma delta, then press Esc.
  2. Press 0 to go to the start, then w w to land on beta.
  3. Press cw and type BETA — notice you are typing in the middle of the line, not at the end.
  4. Press Esc, then daw to delete a whole word with its space, then . to delete another one.
  5. Press 3x, then . — three more characters gone, then three more.
  6. Press ~ a few times and watch the case flip, character by character.
  7. Press dd to clear the whole thing, then i to get back to typing.

Then in a code editor with Vim mode on: press u until you are back where you started, and CtrlR to walk forwards again. Try 3dd, then u.

You now know: count, operator, motion is one sentence — and every new motion you learn multiplies the commands you already have.

Lesson 2 — Moving Around · Lesson 4 — Text Objects