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:
ddelete,cchange,yyank. - 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
| Operator | Says | Where it works |
|---|---|---|
d | delete | Both |
c | change — delete, then leave you typing | Both |
y | yank, which is Vim's word for copy (Lesson 5) | Both |
> < | indent / outdent | Code 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
| Press | Reads as | What you get |
|---|---|---|
dw | delete word | removes the word ahead and the space after it |
de | delete to end of word | removes the word, leaves the space |
d$ | delete to end of line | clears everything from the cursor rightwards |
3dw | delete three words | one keystroke more than dw, three times the work |
dt/ | delete till slash | stops 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.
| Press | Does | Where it works |
|---|---|---|
dd | delete the whole line — in a terminal see the note below | Both |
cc | clear the line and start typing | Both |
yy | copy the whole line | Both |
3yy | with a count, that many lines — a yank may cross rows | Both |
2dd | with a count, that many lines | Code 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.
| Press | Does |
|---|---|
x | delete the character under the cursor — 3x for three |
X | delete the character before it |
r | replace one character: r then the new one, and you stay in normal mode |
s | substitute — delete the character and start typing |
D | delete to the end of the line |
C | change 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.
| Press | Does | Where it works |
|---|---|---|
~ | flip the case of the character under the cursor — 5~ for five | Both |
guiw | lowercase the word you are on | Both |
gUiw | uppercase the word you are on | Both |
g~e | flip case from here to the end of the word | Both |
guu gUU | lowercase / uppercase the whole line | Both |
u U ~ in visual mode | lowercase / uppercase / flip the selection | Both |
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 there3xthen2.means2x. 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
| Press | Does |
|---|---|
u | undo |
| CtrlR | redo |
J | join this line and the next |
:42 | jump to line 42 |
:%s/old/new/g | substitute across the file |
:sort | sort the lines |
:noh | clear 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:
- Type
echo alpha beta gamma delta, then pressEsc. - Press
0to go to the start, thenwwto land onbeta. - Press
cwand typeBETA— notice you are typing in the middle of the line, not at the end. - Press
Esc, thendawto delete a whole word with its space, then.to delete another one. - Press
3x, then.— three more characters gone, then three more. - Press
~a few times and watch the case flip, character by character. - Press
ddto clear the whole thing, thenito 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.
Related
- Vim Mode reference — the full command tables for both halves
- Code Editor
- Terminals
- CLI Coding Agents