Delete, Copy, and Paste in Vim
In the earlier Vim lessons, you learned how to move around a file. This time, you will stay in Normal mode and use that movement to edit text quickly with only the keyboard.
What You Will Do
- Learn delete commands (
x, dd, dw)
- Copy and paste with
yy, p, P
- Use undo/redo (
u, Ctrl+r)
- Combine commands with counts and repeat (
.)
Requirements
- A terminal with Vim installed
- A practice text file
Get Ready
Create a test file
cd ~
cat > test-edit.txt << 'EOF'
First line here
Second line with words
Third line for testing
Fourth line content
Fifth line is last
EOF
Open it in Vim
Launch Vim with the file.
Ready. Press Replay to run the scripted session.
If your screen stops matching the examples later, press u until the file returns to its original state, or quit without saving and reopen test-edit.txt.
Exercise 1: Delete
x – delete a single character
Start position: Beginning of Line 1 (the F).
x deletes the character under the cursor.
Ready. Press Replay to run the scripted session.
| Key |
Result |
Meaning |
x |
irst line here |
Deleted F |
x |
rst line here |
Deleted i |
u |
First line here |
Undo to original |
dw – delete one word
Start position: Beginning of Line 2 (the S in Second).
dw deletes from the cursor to the start of the next word. For beginners, the easiest way to think about it is: place the cursor on the first letter of a word, then use dw to remove that word.
Ready. Press Replay to run the scripted session.
| Key |
Result |
Meaning |
dw |
line with words |
Deleted Second |
u |
Second line with words |
Undo to original |
dd – delete an entire line
Start position: Any character on Line 1.
dd removes the current line. Vim also remembers deleted text, so you can paste it later with p or P.
Ready. Press Replay to run the scripted session.
| Key |
Result |
Description |
dd |
Removes Line 1 |
Entire line deleted |
u |
Restores the line |
Undo |
Exercise 2: Copy (Yank)
yy – copy the current line
Start position: Any character on Line 1.
yy copies the entire line into Vim's memory. Nothing changes on screen until you paste.
Ready. Press Replay to run the scripted session.
| Key |
Result |
Description |
yy |
(no change) |
Current line copied |
p |
Same line inserted below |
Paste after the cursor |
u |
Undo |
Back to original |
Exercise 3: Paste
p vs P
Setup: Copy Line 1 with yy, then move to Line 3 with 3G.
Ready. Press Replay to run the scripted session.
p pastes after the cursor, so the copied line appears below Line 3.
P – paste before the cursor
Ready. Press Replay to run the scripted session.
| Key |
Result |
Note |
P |
Inserts above the cursor |
Paste before |
u |
Undo |
Restore the file |
Exercise 4: Undo and Redo
u – Undo
Start position: Beginning of Line 1.
Ready. Press Replay to run the scripted session.
| Key |
Result |
Description |
dd |
Delete Line 1 |
“First line here” removed |
dd |
Delete Line 2 |
“Second line…” removed |
u |
Undo last delete |
Restores Line 2 |
u |
Undo previous delete |
Restores Line 1 |
Ctrl+r – Redo
Ctrl+r moves forward again after an undo. It is useful when you undo too much and want to reapply the last change.
Ready. Press Replay to run the scripted session.
| Key |
Result |
Description |
dd |
Delete line |
Remove Line 1 |
u |
Undo |
Bring it back |
Ctrl+r |
Redo |
Delete it again |
Exercise 5: Combine with Counts
3dd – delete three lines at once
Now that the single commands feel familiar, you can combine them for speed.
Ready. Press Replay to run the scripted session.
| Key |
Result |
Description |
3dd |
Deletes lines 1–3 |
Count + command |
u |
Undo |
Restore lines |
Exercise 6: Repeat with .
. (dot) repeats the last command
. repeats your last edit. It does not repeat cursor movement by itself, so think of it as “do the same change again here.”
Ready. Press Replay to run the scripted session.
| Key |
Result |
Description |
dd |
Delete Line 1 |
Recent command |
. |
Delete Line 2 |
Repeats dd |
. |
Delete Line 3 |
Repeats again |
Editing Command Cheat Sheet
Delete
| Command |
Description |
x |
Delete one character |
dd |
Delete current line |
dw |
Delete current word |
d$ |
Delete from cursor to end of line |
d0 |
Delete from cursor to start of line |
D |
Same as d$ |
Copy (Yank)
| Command |
Description |
yy |
Yank current line |
yw |
Yank current word |
y$ |
Yank to end of line |
y0 |
Yank to start of line |
Paste
| Command |
Description |
p |
Paste after the cursor |
P |
Paste before the cursor |
Undo / Redo
| Command |
Description |
u |
Undo |
Ctrl+r |
Redo |
Counts
| Command |
Description |
3dd |
Delete three lines |
5x |
Delete five characters |
2yy |
Copy two lines |
3p |
Paste three times |
Repeat
| Command |
Description |
. |
Repeat last command |
Troubleshooting
Issue 1: Paste shows unexpected text
Cause: A previous delete replaced the text that Vim remembered most recently.
Fix: Deletes also make text available to paste. Run yy again and paste immediately.
Issue 2: Undo went too far
Fix:
Ctrl+r # redo to move forward again
Issue 3: Mixing up p and P
Rule of thumb:
p = put after
P = Put before
Practice Checklist
Next Episode
👉 Vim Series 5: Configure Vim—Create .vimrc and Real-World Tips
💬 댓글
이 글에 대한 의견을 남겨주세요