Vim operators: delete, change, copy and change case

An operator is a verb. d deletes, c changes, y copies and gU uppercases, and the motion you type after it says how far. Learn six operators and every motion you already know becomes six new edits: dw, c$, yf,, gUiw.

Each example shows a line before and after, with the keys in between. Unless a row says otherwise, the cursor starts on the first character. Every example was replayed in the Vim engine that runs the game and checked in Neovim.

The grammar: operator + motion

A Vim edit reads like a short sentence: verb, then how far. The verb is one of these operators:

OperatorWhat it does to the text it covers
ddeletes it
cdeletes it and starts Insert mode, so you can type the replacement
ycopies ("yanks") it, leaving the text as it was
gUmakes it UPPERCASE
gumakes it lowercase
g~toggles the case of each letter

"How far" is any motion: w to the next word, $ to the end of the line, t) to just before the next ), G to the last line. The operator covers the text from the cursor to wherever that motion would have moved it.

Counts go in front of the operator, in front of the motion, or both. 2dw and d2w both delete two words. Two counts multiply: 2d3w on a b c d e f g h deletes six words and leaves g h.

Delete in Vim: d

Here is d with a few motions on the quick brown fox jumps:

KeysCursor starts onAfter
dwt of thequick brown fox jumps
d2wt of thebrown fox jumps
det of the quick brown fox jumps (the space stays)
d$ or Db of brownthe quick 
d0b of brownbrown fox jumps
dbb of brownthe brown fox jumps

Backward motions like b and 0 delete the text behind the cursor and leave the character under it alone. That is why d0 keeps the b of brown.

How to delete a word in Vim

daw and diw use text objects, which work wherever the cursor stands inside the word. That makes daw the safest choice when you are not sure the cursor is on the first letter.

Why dw and de stop in different places

Look at dw and de above. w moves to the first letter of quick, but dw does not delete that letter. e moves to the last letter of the, and de does delete it. The difference is built into each motion:

Backward motions (b, F, T, 0, ^, h) are all exclusive: they delete the character they land on but never the one under the cursor.

You rarely have to think about it, because the result is nearly always what you meant. It shows most with t and f:

BeforeKeysAfter
red, green, bluedt,, green, blue
red, green, bluedf, green, blue
red, green, bluedf and a spacegreen, blue
call(a, b)f(ldt)call()
call(a, b)f(ldf)call(

Use t when you want to keep the character you are aiming at, and f when it should go too.

Delete lines: dd, counts and linewise motions

Type an operator twice and it works on the whole line: dd deletes the line, and 3dd deletes three lines starting with this one. Motions that move between lines, such as j, k, G and gg, are linewise: after an operator they cover whole lines, whatever column the cursor is on.

In each row the buffer holds the five lines one to five, and the cursor starts on two:

KeysDeletesLines left
ddthis lineone three four five
2ddthis line and the nextone four five
djthis line and the one below, same as 2ddone four five
dkthis line and the one abovethree four five
dGfrom here to the end of the fileone
dggfrom here to the top of the filethree four five

To delete single characters there are two shortcuts: x deletes the character under the cursor (the same as dl) and X the one before it (dh). With a count, 5x on hello world leaves  world.

One special case: when the word is the last one on the line, dw stops at the end of the line instead of joining the next line. On one two, with three on the next line, dw from the t of two leaves one  and keeps three on its own line, even though w alone would jump to three.

Change in Vim: c

c deletes like d and then puts you in Insert mode. Type the new text and press Esc. Because the delete and the typing are one command, u undoes both and . repeats both.

BeforeKeysAfter
the quick brown fox, cursor on the qcwslow<Esc>the slow brown fox
the quick brown fox, cursor on the qc2wred<Esc>the red fox
the quick brown fox, cursor on the iciwslow<Esc>the slow brown fox
return a + b;, cursor on the act;x<Esc>return x;
return a + b;, cursor on the aCx;<Esc> (same as c$)return x;
old lineccnew line<Esc> or Snew line<Esc>new line
catsb<Esc> (same as cl)bat
cat food3sdog<Esc>dog food

Notice the first row. dw deletes the space after a word, but cw does not: on a word, Vim treats cw as ce, because when you change a word you nearly always want to keep the space after it. c2w likewise stops at the end of the second word.

cc and S replace the text of the line but keep the line itself. If the line is indented, Neovim and Vimrealm keep the indent. Vim keeps it only when the autoindent option is on, which it is not by default.

How to change a word in Vim

Copy and paste: y and p

y copies over a motion without changing anything. p pastes after the cursor and P before it. Text copied as whole lines pastes as whole lines, below or above the current one.

BeforeKeysAfter
let a = 1;yyplet a = 1; / let a = 1; (the line is duplicated)
big red boxwywPbig red red box
sum(a, b), cursor on the ayi($psum(a, b)a, b

yy copies the line. In Vim and in Vimrealm, Y does the same. In Neovim, Y copies to the end of the line, like y$, to match D and C.

d, c and x also keep what they deleted, ready for p. So a delete followed by a paste is a move, and a few classic tricks fall out of that:

BeforeKeysAfter
teh, cursor on the expthe (two letters swapped)
second / firstddpfirst / second (two lines swapped)
red big boxdwwPbig red box (two words swapped)

Change case: gU, gu, g~ and ~

The case operators take motions like any other operator. They do not enter Insert mode and they do not store anything for p.

BeforeKeysAfter
make it loudgUwMAKE it loud
make it loudgU2wMAKE IT loud
make it loud, cursor on the i of itgU$make IT LOUD
make it loud, cursor anywheregUU or gUgUMAKE IT LOUD
STOP SHOUTING, cursor on the second wordguiwSTOP shouting
STOP SHOUTINGguu or gugustop shouting
hELLO wORLDg~~ or g~g~Hello World
say "hi there" nowgUi"say "HI THERE" now

~ on its own is not an operator. It toggles the case of the character under the cursor and moves one to the right, so ~ on hello gives Hello, and 3~ gives HELlo.

How to uppercase a word in Vim

Doubled operators and one-key shortcuts

Typing an operator twice applies it to the current line. A few common edits also have a one-key shortcut:

KeysSame asWhat it does
dddelete the line
cc or Schange the line
yy or Ycopy the line (Y is y$ in Neovim)
gUU, guu, g~~gUgU, gugu, g~g~uppercase, lowercase or toggle the line
Dd$delete to the end of the line
Cc$change to the end of the line
xdldelete the character under the cursor
Xdhdelete the character before the cursor
sclchange the character under the cursor

Operators with text objects

After an operator you can also type a text object: i or a followed by what to work on, such as w for a word, " for a quoted string or ( for parentheses. A text object covers a whole thing around the cursor, so the cursor can be anywhere inside it.

BeforeKeysAfter
the quick brown fox, cursor on the idawthe brown fox
msg = "hello"ci"bye<Esc>msg = "bye"
sum(a, b, c), cursor on the adi(sum()

In the second row the cursor starts outside the quotes. Vim looks ahead along the line for the string. Vim text objects explained lists every object, with more examples.

Operators and the dot command

. repeats the last change, and an operator with its motion counts as one change. So once you have made an edit, you can move and press . to make it again:

BeforeKeysAfter
a b c d edaw..d e
id name emailgUiww.w.ID NAME EMAIL
old = old + 1ciwnew<Esc>fo.new = new + 1
f(x) g(y)f(ldt);.f() g()

In the last row ; repeats the t) search, which jumps to the y just before the next ), and . repeats dt) there. Edits that do not depend on the exact column, like ciw and daw, repeat best. The dot command guide has more on planning edits around it.

Real Vim has more

Once d, c and y feel natural, the same grammar works with a few more operators, motions and registers. Vimrealm does not teach these yet:

Vimrealm teaches every operator on this page, apart from the list above, in quests scored by keystrokes. Chapters 1–5, which teach the motions the operators use, are free, with no sign-up.

Play Vimrealm free

Where the commands are taught: x, X, dd and ~ in chapter 6, The Breaking Arts; d, c, D, C, cc, s, S, gu, gU and g~ in chapter 8, Operator Sanctum; text objects in chapter 9; y, p and . in chapter 10; Visual mode and U in chapter 12. These are part of the full version ($7.99, one payment). See how the course works or keep the cheat sheet entry for operators open while you practise.