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. Working out the count is easier with relative line numbers turned on, since Vim then prints the distance to each line for you.
The d operator also accepts the j and k motions. dj deletes the current line and the one below it, and dk deletes the current line and the one above it. Both motions are linewise, so each command removes two whole lines rather than parts of them. Adding a count extends the reach: d3j deletes the current line and the three lines below it, for four lines in total.
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
When the block is long enough that counting line numbers becomes tedious, mark its ends instead of reading them off the screen. Move to the first line and press ma, move to the last line and press mb, then run :'a,'bd. Vim replaces each mark with the line it sits on and deletes the block, including both ends:
:'a,'bdThe mark letter is arbitrary, so a and b are only a convention. Marks survive editing above them, which makes this the safer option when you are still changing the file while deciding what to remove.
Deleting to the Start or End of the File
The d operator accepts the G and gg motions, which reach the bottom and the top of the file:
dG- Delete from the current line to the end of the filedgg- Delete from the current line to the beginning of the file
Both motions are linewise, so the current line is always part of the deletion. With the cursor on line 5 of an eight-line file, dG leaves lines 1 through 4, and dgg leaves lines 6 through 8.
These are the normal mode equivalents of :.,$d and :1,.d. The normal mode form is faster to type from the keyboard, while the ex form states the range explicitly, which reads better inside a macro or a script where nobody can see where the cursor was.
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 then deletes from there to the end of the file, which is every line. When you want to act on the whole file rather than delete it, for example to copy or reindent it, see how to select all in Vim
.
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/dVim also provides :v, which the documentation defines as being the same as :g!. The two are interchangeable, so use whichever you find clearer:
:v/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.
Every one of these commands leaves the last removed line in the unnamed register, and with clipboard set to unnamed or unnamedplus it also overwrites whatever you had copied. Sending the deleted text to the black hole register avoids that, and Vim runs the command faster because it is not filling registers on every match:
:g/pattern/d _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 |
dj | Delete the current line and the one below it |
dk | Delete the current line and the one above it |
V then d | Select lines visually and delete |
:3,5d | Delete lines 3 through 5 |
:'a,'bd | Delete from mark a to mark b |
:.,$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 |
:v/pattern/d | Shorthand for :g!/pattern/d |
:g/pattern/d _ | Delete matching lines without touching the registers |
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. See how to copy, cut, and paste in Vim
for the named registers and the rest of the yank commands.
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