C.W.K.
Stream
Lesson 01 of 06 · published

The Grammar — Operator + Motion

~13 min · vim, grammar, composability

Level 0Trapped
0 XP0/35 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Vim is a language, not a key list

The reason Vim seems to have a thousand commands is that you can combine a small number of operators with a small number of motions and get hundreds of verbs for free. You don't memorize delete-to-next-word; you memorize d (delete) and w (next word), and the combination dw follows automatically.

The formula

[count] {operator} [count] {motion}

Count is optional. Both counts multiply each other (3d2w deletes six words, equal to d6w or 6dw). Operator says what; motion says how far. That's the whole grammar.

The five operators you'll use most

  • d — delete (also yanks).
  • c — change (delete + enter Insert mode).
  • y — yank (copy without removing).
  • > — indent right.
  • < — indent left.

And the second tier worth knowing day-one: = (auto-format), gu / gU (case fold), g~ (toggle case), gq (text reflow).

Doubling = operate on line

Vim has a beautiful shortcut: pressing an operator twice applies it to the entire current line. dd deletes the line, yy yanks it, cc changes it, >> indents it. The doubled key is shorthand for operator + line.

Composability is the entire point. Memorizing dw, de, db, d$, d^, dG is exhausting. Memorizing d and the motion list is two lookups that combine for free. Vim feels infinite because it actually is — anything that's a motion is also a delete-target.

Capital and shifted forms

Many operators have a capital that means "to end of line": D = d$, C = c$, Y historically = yy (linewise yank). The exception is Y — modern Neovim defaults to Y = y$ for consistency, while classic Vim still does Y = yy. Worth knowing if you switch between machines.

Code

Operator + motion = a verb·vim
dw      " delete to next word
d3w     " delete next 3 words
d$      " delete to end of line
d0      " delete to start of line
dG      " delete from here to end of file
cw      " change to next word (delete + insert)
c$      " change to end of line (same as C)
yiw     " yank inner word (text-object preview)
>>      " indent line right
<<      " indent line left
=ap     " auto-format around paragraph
Doubling — the linewise shortcut·vim
dd     " delete current line
yy     " yank current line
cc     " change current line
>>     " indent current line
<<     " dedent current line
==     " auto-indent current line
guu    " lowercase current line
gUU    " uppercase current line
~~     " " toggle case of line (some configs)
Counts compose with the grammar·vim
5dd     " delete 5 lines
3>>     " indent 3 lines
10x     " delete 10 characters
d3w     " delete 3 words (same as 3dw)
2d3w    " delete 6 words

External links

Exercise

Open a source file. Predict before pressing each key what the result will be: (1) d3w, (2) c$, (3) 5dd, (4) guu, (5) =ap. If you can describe each effect in one sentence before pressing, the grammar has clicked.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.