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:
| Key | Action | Mnemonic |
|---|---|---|
x | Delete the character under cursor | X marks the spot |
dd | Delete the entire line | delete twice |
D | Delete from cursor to end of line | Delete 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:
| Key | Action |
|---|---|
u | Undo |
Ctrl+r | Redo |
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:
| Key | Action | Mnemonic |
|---|---|---|
yy | Yank the whole line | yank |
p | Paste below | put after |
P | Paste above | Put 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:
| Key | Action |
|---|---|
x | One character under the cursor |
X | One character before cursor (Backspace-like) |
dd | Entire line |
D | From cursor to end of line |
3dd | Delete 3 lines |
dw | Delete to the next word |
d$ | Delete to end of line (= D) |
d0 | Delete 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
- Open the file with
vim todo.txt. - Delete a line with
dd. - Undo with
u, then redo withCtrl+r. - Yank the "Check email" line with
yy, move to the end of the file, and paste withp. - Cut the "Prep deployment" line with
dd, move below "Review code", and paste withpto reorder. - Press
ddonce, then repeat with.to delete several more lines in a row. - Press
umultiple 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?