Vim operators and motions: a grammar
GoByte Skills #25: Vim commands are a verb plus a noun. d waits for a motion, 2w says how far, and every new motion works with every operator. Plus why cw breaks the pattern on purpose.
Zoe is one of GoByte's characters. This post was drafted by AI agents in Zoe's voice, then fact checked, run and edited by the GoByte team.
Vim commands are a verb plus a noun, like d2w, so a few operators and motions give hundreds of edits. An operator such as d, c, y or gU does nothing alone. It puts Vim in operator pending mode and waits for a motion, then acts on the text between the cursor and where that motion would land.
Transcript
A line of code sits in a buffer while operator and motion tiles snap together into commands like d2w and di(, and the cursor and highlighted range show exactly which text each sentence deletes or changes.
echo 'let total = price * (qty + tax)' > t.txt
vim -Nu NONE -n -es -c 'normal d2w' -c wq t.txt
cat t.txt
# = price * (qty + tax)
Same line, other sentences (cursor starts on l):
wcwsum<Esc> let sum = price * (qty + tax)
f(di( let total = price * ()
fqd$ let total = price * (
3wgUiw let total = PRICE * (qty + tax)
The animation chains d2w, f(di( and FpgUiw on one line instead (Fp finds p backward), ending at = PRICE * ().
Why it scales#
:help operator lists 16 operators; ~ is one only when 'tildeop' is set, so 15 by default. Each accepts any motion (w, $, f(, /qty<CR>) and any text object (iw, i(, a"). Learn one new motion and every operator gains it for free: multiplication, not memorization. Counts multiply too, so 2d3w deletes six words.
The motion decides the shape#
j is linewise, so dj deletes two whole lines. t is exclusive and f is inclusive: on our line dtp leaves price, dfp leaves rice. Doubling an operator means "this line": dd, yy, gUgU.
The boundary#
cwis a documented special case: on a word it acts likeceand keeps the trailing blanks. Onone two,cwXgivesX twowhiledwiXgivesXtwo.- A bare
dwon the last word of a line stops at the line end. A count crosses it: onone two/three four,d2wfromtwoleavesone four. - Text objects are not motions. They work only after an operator or in Visual mode: typing
iwalone enters Insert mode and types aw.
Rule of thumb#
When you reach for Visual mode to select and then delete, say the edit as a sentence instead. "Delete inside parens" is di(, and it works from anywhere inside them. Vim's grammar fits on an index card, which is why people spend a decade on the vocabulary.
Your product here? Partner with us
Back to topDiscussion
No comments yet. Signed in GoByte members with a verified e-mail can join. Community guidelines
Reading is open to everyone. Commenting and voting need a GoByte account with a verified e-mail.