DaleSchool

Basic Editing

Beginner15min

Learning Objectives

  • Delete with x and dd
  • Undo and redo with u and Ctrl+r
  • Copy and paste with yy and p
  • Repeat the last edit with the . command

Working Code

Create a practice TODO file:

cat << 'EOF' > todo.txt
# Today's tasks
- Check email
- Review code
- Fix bugs
- Write tests
- Prep deployment
- Attend meeting
EOF
vim todo.txt

Try deleting

From Normal mode, try the delete commands:

KeyActionMnemonic
xDelete the character under cursorX marks the spot
ddDelete the entire linedelete twice
DDelete from cursor to end of lineDelete to end

Place your cursor on the "Attend meeting" line and press dd. The whole line vanishes!

On the "Check email" line, press D. Only the part from the cursor to the end of the line disappears.

Undo

Made a mistake? No worries:

KeyAction
uUndo
Ctrl+rRedo

Press u to bring back what you just deleted. Each press steps further back in history. If you went too far, Ctrl+r moves forward again.

Vim's undo is unlimited. You can roll back everything you've done since opening the file.

Try It Yourself

Copy and paste

In Vim, copying is called yanking:

KeyActionMnemonic
yyYank the whole lineyank
pPaste belowput after
PPaste abovePut before

Place your cursor on the "Review code" line and try:

yy    → yank the "Review code" line
p     → paste below

Result:

- Review code
- Review code    ← pasted copy!
- Fix bugs

A line deleted with dd is really "cut." After dd, move elsewhere and press p to paste it there. Handy for reordering lines:

dd    → cut the "Fix bugs" line
k     → move up
p     → paste here

The dot (.) command — "one more time!"

. repeats the last edit:

dd    → delete the "Write tests" line
.     → delete the next line (repeat dd)
.     → and the next one too

When you want to clear several finished TODO items in a row, one dd plus a few . does the job.

It's just as handy when cleaning up config files:

# cleaning up comments in nginx.conf
server {
    # unnecessary comment
    listen 80;
    # another one to delete
    server_name example.com;
    # this one too
    root /var/www/html;
}

dd on the first comment, move down to the next one and press ., then again — fast, right?

"Why?" — Why deleting is also cutting

Vim has no separate "cut" command. dd both deletes and cuts. The deleted text goes into a temporary storage called a register.

Why is that great?

1. dd deletes the line (= saved to a register)
2. Move wherever you want
3. p pastes it

"Delete, then paste elsewhere" just flows. It's more intuitive than cut-then-paste in a regular editor.

Delete commands summary

Common delete commands at a glance:

KeyAction
xOne character under the cursor
XOne character before cursor (Backspace-like)
ddEntire line
DFrom cursor to end of line
3ddDelete 3 lines
dwDelete to the next word
d$Delete to end of line (= D)
d0Delete to start of line

You'll learn more d + motion combos in the next lesson.

Repeat with number prefixes

Numbers work with deletes and yanks too:

3dd   → delete 3 lines
5yy   → yank 5 lines
3x    → delete 3 characters

To clear five finished items in a row, just type 5dd!

Deep Dive

  1. Open the file with vim todo.txt.
  2. Delete a line with dd.
  3. Undo with u, then redo with Ctrl+r.
  4. Yank the "Check email" line with yy, move to the end of the file, and paste with p.
  5. Cut the "Prep deployment" line with dd, move below "Review code", and paste with p to reorder.
  6. Press dd once, then repeat with . to delete several more lines in a row.
  7. Press u multiple times to undo everything, then :q! to exit.

Quiz

Which command deletes the entire current line?

You pressed undo (u) too many times. How do you move forward again?

What does the . (dot) command do?