Editing Basics
You already know how to get into Insert mode and type. This chapter covers the Normal-mode commands that make Vim worth learning: deleting, copying, pasting, undoing, and repeating, all without touching Insert mode most of the time.
Deleting
x deletes the character under the cursor. dd deletes the whole line.
dw deletes to the end of the current word. These follow a pattern
worth noticing early: d is an operator that means “delete,” and it
combines with a motion. dw is “delete” plus “word.” d$ is “delete” plus
“to end of line.” Once this clicks, you can combine d with any motion
from the last chapter: d} deletes to the end of the paragraph, d3j
deletes the current line plus the next three.
Copying (yanking) and pasting
Vim calls copying “yanking,” and the key is y. It works the same way d
does: yy yanks the whole line, yw yanks a word, y$ yanks to the end
of the line. To paste, use p to put the yanked text after the cursor, or
P to put it before.
Undo and redo
u undoes the last change. Ctrl-r redoes it. Vim’s undo history is
deep. You can usually press u many times in a row and step all the way
back to when you opened the file.
Counts still apply
Everything from the counts trick in the last chapter works here too. 3dd
deletes three lines. 2yy yanks two lines. 5x deletes five characters.
The repeat command
. repeats your last change. This is one of Vim’s best tricks: make an
edit once, then move to the next place you want the same edit and press
.. If you just deleted a word with dw, moving somewhere else and
pressing . deletes another word there.
Changing text
c works like d but drops you into Insert mode afterward, useful when
you want to replace something rather than just remove it. cw deletes a
word and lets you type a replacement. cc clears the whole line and
leaves you ready to type a new one.
Try it
Open a scratch file with a few lines of throwaway text (or use this chapter’s own file, just don’t save your changes) and work through these:
- Delete a single character with
x, then undo it withu. - Delete an entire line with
dd. Undo it. - Yank a line with
yy, move down a couple of lines, and paste it withp. - Delete a word with
dw, move to a different word, and press.to repeat the deletion there. - Use
cwon a word to replace it with something else, without manually deleting it first and switching to Insert mode. - Try
3ddto delete three lines at once.