How to Delete Lines in Vim / Vi

Vim is a powerful text editor that comes preinstalled on most Linux distributions and macOS. One of the most common editing tasks is deleting lines, whether it is a single line, a range, or all lines matching a pattern.
This guide explains how to delete lines in Vim using normal mode commands, ex commands, and the global command.
Deleting a Single Line
The dd command deletes the entire line where the cursor is located:
- Press
Escto make sure you are in normal mode. - Place the cursor on the line you want to delete.
- Type
dd.
The line is removed immediately. You do not need to press Enter, as dd is a normal mode command that executes as soon as you type it.
Deleting Multiple Lines
To delete multiple consecutive lines, prepend dd with a count. For example, to delete five lines:
- Press
Escto go to normal mode. - Place the cursor on the first line you want to delete.
- Type
5dd.
This deletes the current line and the four lines below it.
Using Visual Mode
You can also select lines visually before deleting:
- Press
V(uppercase) to enter visual line mode. - Use
jorkto extend the selection up or down. - Press
dto delete the selected lines.
Deleting a Range of Lines
To delete a specific range, use the ex command :d with a line range:
:[start],[end]dFor example, to delete lines 3 through 5:
- Press
Escto go to normal mode. - Type
:3,5dand press Enter.
You can use the following special characters in the range:
.(dot) - The current line$- The last line%- All lines
Here are a few examples:
:.,$d- Delete from the current line to the end of the file:1,.d- Delete from the beginning of the file to the current line:10,$d- Delete from line 10 to the end of the file
Deleting All Lines
To delete all lines in the file, use % which represents the entire file:
- Press
Escto go to normal mode. - Type
:%dand press Enter.
You can also use gg followed by dG. The gg command moves the cursor to the first line, and dG deletes from the current line to the end of the file.
Deleting Parts of a Line
Vim also provides commands to delete portions of a line without removing the entire line:
Dord$- Delete from the cursor to the end of the lined0- Delete from the cursor to the beginning of the linedw- Delete from the cursor to the start of the next worddb- Delete from the cursor to the beginning of the current word
Deleting Lines by Pattern
The global command (:g) lets you delete all lines that match a pattern:
:g/pattern/dTo delete lines that do not match the pattern, add an exclamation mark:
:g!/pattern/dThe pattern can be a literal string or a regular expression . Here are some examples:
:g/foo/d- Delete all lines containing “foo”. This also matches lines where “foo” is part of a larger word, such as “football”.:g!/foo/d- Delete all lines that do not contain “foo”.:g/^#/d- Delete all lines beginning with#(useful for removing comments).:g/^$/d- Delete all empty lines.:g/^\s*$/d- Delete all blank lines, including lines that contain only whitespace.
Undoing a Delete
If you delete a line by mistake, press u in normal mode to undo the last change. You can press u multiple times to undo several changes. To redo an undone change, press Ctrl+r. For a deeper explanation, see How to Undo and Redo in Vim
.
Quick Reference
For a printable quick reference, see the Vim cheatsheet .
| Command | Description |
|---|---|
dd | Delete the current line |
5dd | Delete 5 lines starting from the current line |
V then d | Select lines visually and delete |
:3,5d | Delete lines 3 through 5 |
:.,$d | Delete from current line to end of file |
:1,.d | Delete from beginning of file to current line |
:%d | Delete all lines |
dG | Delete from current line to end of file |
dgg | Delete from current line to beginning of file |
D | Delete from cursor to end of line |
d0 | Delete from cursor to beginning of line |
:g/pattern/d | Delete all lines matching a pattern |
:g!/pattern/d | Delete all lines not matching a pattern |
u | Undo last delete |
FAQ
Does dd copy the deleted line?
Yes. Vim places deleted text into the default register. You can paste it with p (after the cursor) or P (before the cursor). This makes dd + p a quick way to move a line.
How do I delete a line without saving it to a register?
Use the black hole register by typing "_dd. This deletes the line without overwriting the contents of the default register.
Can I delete lines matching a pattern in a specific range?
Yes. Use :10,20g/pattern/d to delete matching lines only between lines 10 and 20.
What is the difference between d and D?
Lowercase d is an operator that waits for a motion (e.g., dd, dw, d$). Uppercase D is a shortcut for d$ and immediately deletes from the cursor to the end of the line.
Conclusion
When deleting large blocks, prefer :%d or :g/pattern/d over visual mode. They are scriptable and work inside macros and .vimrc autocommands.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page