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:
| Operator | What it does to the text it covers |
|---|---|
d | deletes it |
c | deletes it and starts Insert mode, so you can type the replacement |
y | copies ("yanks") it, leaving the text as it was |
gU | makes it UPPERCASE |
gu | makes 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:
| Keys | Cursor starts on | After |
|---|---|---|
dw | t of the | quick brown fox jumps |
d2w | t of the | brown fox jumps |
de | t of the | quick brown fox jumps (the space stays) |
d$ or D | b of brown | the quick |
d0 | b of brown | brown fox jumps |
db | b of brown | the 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
- From the start of the word:
dw. It deletes the word and the space after it. - From anywhere inside the word:
dawdeletes the word and the space after it, so the sentence closes up.diwdeletes only the word and leaves both spaces. - The word before the cursor:
db, or Ctrl-w while you are typing in Insert mode. - Several words: put a count in, as in
d3w.
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:
- Exclusive motions stop the operator just before the character they land on:
w,W,t,l. - Inclusive motions include the character they land on:
e,E,f,$,%.
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:
| Before | Keys | After |
|---|---|---|
red, green, blue | dt, | , green, blue |
red, green, blue | df, | green, blue |
red, green, blue | df and a space | green, 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:
| Keys | Deletes | Lines left |
|---|---|---|
dd | this line | one three four five |
2dd | this line and the next | one four five |
dj | this line and the one below, same as 2dd | one four five |
dk | this line and the one above | three four five |
dG | from here to the end of the file | one |
dgg | from here to the top of the file | three 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.
| Before | Keys | After |
|---|---|---|
the quick brown fox, cursor on the q | cwslow<Esc> | the slow brown fox |
the quick brown fox, cursor on the q | c2wred<Esc> | the red fox |
the quick brown fox, cursor on the i | ciwslow<Esc> | the slow brown fox |
return a + b;, cursor on the a | ct;x<Esc> | return x; |
return a + b;, cursor on the a | Cx;<Esc> (same as c$) | return x; |
old line | ccnew line<Esc> or Snew line<Esc> | new line |
cat | sb<Esc> (same as cl) | bat |
cat food | 3sdog<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
- From the start of the word:
cw, type the new word, Esc. - From anywhere inside it:
ciw. You do not need to move to the first letter, and.repeats it on the next word no matter where the cursor lands. - The rest of the line:
C. Up to a character:ctand the character, as inct;orct).
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.
| Before | Keys | After |
|---|---|---|
let a = 1; | yyp | let a = 1; / let a = 1; (the line is duplicated) |
big red box | wywP | big red red box |
sum(a, b), cursor on the a | yi($p | sum(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:
| Before | Keys | After |
|---|---|---|
teh, cursor on the e | xp | the (two letters swapped) |
second / first | ddp | first / second (two lines swapped) |
red big box | dwwP | big 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.
| Before | Keys | After |
|---|---|---|
make it loud | gUw | MAKE it loud |
make it loud | gU2w | MAKE IT loud |
make it loud, cursor on the i of it | gU$ | make IT LOUD |
make it loud, cursor anywhere | gUU or gUgU | MAKE IT LOUD |
STOP SHOUTING, cursor on the second word | guiw | STOP shouting |
STOP SHOUTING | guu or gugu | stop shouting |
hELLO wORLD | g~~ or g~g~ | Hello World |
say "hi there" now | gUi" | 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
- The word under the cursor, from anywhere in it:
gUiw. Useguiwfor lowercase. - From the start of the word:
gUw. - Only the first letter:
~on that letter, orgUlif it must end up uppercase whatever it was. - The whole line:
gUU. - A selection: select it in Visual mode with
vand pressU(orufor lowercase).
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:
| Keys | Same as | What it does |
|---|---|---|
dd | delete the line | |
cc or S | change the line | |
yy or Y | copy the line (Y is y$ in Neovim) | |
gUU, guu, g~~ | gUgU, gugu, g~g~ | uppercase, lowercase or toggle the line |
D | d$ | delete to the end of the line |
C | c$ | change to the end of the line |
x | dl | delete the character under the cursor |
X | dh | delete the character before the cursor |
s | cl | change 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.
| Before | Keys | After |
|---|---|---|
the quick brown fox, cursor on the i | daw | the brown fox |
msg = "hello" | ci"bye<Esc> | msg = "bye" |
sum(a, b, c), cursor on the a | di( | 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:
| Before | Keys | After |
|---|---|---|
a b c d e | daw.. | d e |
id name email | gUiww.w. | ID NAME EMAIL |
old = old + 1 | ciwnew<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:
>and<: indent and unindent, as in>>for one line or>}for a paragraph=: re-indent, as in=Gto the end of the filegq: wrap text to the line width, as ingqq- Searches as motions:
d/endEnter deletes up to the nextend - Named registers:
"ayycopies the line into registera, and"appastes it, so you can keep several things copied at once
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 freeWhere 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.