Skip to content

Vim operators: dw, cw, ci" and the dot command

AdvancedLesson 39 of 6610 min

Edit like a Vim user: delete and change with d and c plus a motion, ci" to change inside quotes, counts like 3dd, and . to repeat the last change.

Vim's editing keys work like a small language: an operator (what to do) followed by a motion (how far). Once you know a few of each, you can combine them without learning anything new.

OperatorDoes
ddelete
cchange: delete, then start typing
yyank (copy)

Put any motion after it: dw deletes to the next word, d$ to the end of the line, dG to the end of the file, cw changes the rest of the word. Press the operator twice for the whole line: dd deletes a line, cc changes it, yy copies it. p pastes what you deleted or copied, after the cursor (or below, for lines).

vim deploy.yml

Counts

A number in front repeats: 3dd deletes three lines, d2w two words, 5x five characters.

Text objects: inside and around

After an operator, i means inside and a around something:

KeysChanges or deletes
ciwthe word under the cursor, wherever in it you are
ci"the text inside the quotes on this line
ca"the text and the quotes too
ci( / ci{inside brackets or braces
dapa whole paragraph, with the blank line after it

ci" is a favourite: the cursor can be anywhere on the line before the quotes, and Vim finds them.

The dot command

. repeats your last change. Change one word with cwnew and Esc, move to the next place (n after a search is perfect for this), and press . to make the same change again. Search, change, then n.n.n.: that's how Vim users rename things.

Your turn

In deploy.yml: (1) change the host inside the quotes from old.example.com to shop.example.com; (2) delete the three legacy_ lines; (3) change every staging to production, using . to repeat the change. Save and quit.

Next