Operators: delete, change, yank, put

Vim · Essentials · 12 min

Operator + motion is the grammar of Vim; once it clicks you can combine every verb with every motion.

Vim commands form a little language: operator + motion. The operator is the verb, the motion says how much text.

  • d deletes, c changes (deletes, then enters Insert mode), y yanks (copies).
  • > and < indent and dedent, gu/gU lowercase/uppercase, = re-indents.

Combine them with any motion: dw deletes to the next word, d$ to end of line, dG to end of file, y} yanks a paragraph, c3w changes three words. Doubling the operator acts on the whole line: dd, yy, cc, >>. A count applies too: 3dd deletes three lines.

Some shortcuts are so common they get their own key: x = dl (delete character), D = d$, C = c$, Y = yy (in Vim; Neovim maps it to y$).

Deleted and yanked text goes into a register, and you put it back with p (after the cursor, or below the line for linewise text) or P (before). So dd then p moves a line down; xp swaps two characters; ddkP moves a line up.

Note that cw is special: on a word it behaves like ce and keeps the trailing space. Also, deleting with d overwrites what you yanked earlier, which surprises everyone once; the registers lesson shows how to get it back.

Example

dw      " delete to start of next word
d$      " delete to end of line (same as D)
c2w     " change two words, then type the replacement
yy      " yank (copy) the current line
p       " put it below the current line
3dd     " delete three lines
>>      " indent the line
gUw     " uppercase to end of word
ddp     " swap the current line with the one below

Remember

  • Grammar: [count] operator motion, e.g. 2dw or d2w
  • Doubled operator = whole line: dd, yy, cc, >>
  • p puts after/below, P puts before/above

Watch out for

  • Expecting p to paste what you yanked after you deleted something else; d overwrites the unnamed register.

Check yourself

  1. What does `c$` do?
  2. Which command swaps the current line with the line below it?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/change.txt.html