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.
With relative line numbers turned on, the number you see next to the target line is the count for j or k. d4j deletes through the line marked 4, the same five lines as 5dd.
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
To delete from the cursor to the end of the line, press D. The character under the cursor goes too, but the text to its left stays on the 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 everything before the cursor, back to the start of the lined^- Delete back to the first non-blank character from inside the text, keeping the indentationdw- Delete from the cursor to the start of the next worddb- Delete back to the start of the word, or the previous word when the cursor is on a word’s first letter
When d0, d^, or db moves backward within a line, Vim keeps the character under the cursor. With the cursor inside the indentation, d^ deletes forward up to the first non-blank character, including the whitespace under the cursor.
On the line server_name example.com www.example.com;, place the cursor on the first w and type d0. Vim deletes everything to the left of the cursor and leaves you with www.example.com;.
A count before D extends the deletion into the lines below. With the cursor on the colon in alpha:beta and another line below it, 2D leaves alpha and removes the next line. If the cursor starts at or before the first non-blank character, 2D can remove both lines entirely.
To empty a line but keep it in the file, type 0D. The 0 moves the cursor to the first column, and D deletes from there to the end of the line. cc and S also clear the line and put you in insert mode, ready to type the replacement. If autoindent is set, both commands keep the line’s indentation.
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. With Vim’s default undo behavior, press u repeatedly to step back through changes and Ctrl+r to redo an undone change.
If cpoptions includes the u flag, pressing u a second time reapplies the deletion. You may encounter this when vi launches vim.tiny in compatible mode on Debian or Ubuntu Desktop. To step back through several changes in this mode, press u once, then press Ctrl+r for each earlier change. See How to Undo and Redo in Vim
for ways to change this setting.
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 everything before the cursor |
d^ | Delete back to the first non-blank character from inside the text |
0D | Clear the line and keep it |
cc | Clear the line and enter insert mode |
: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?
Press d followed by a motion, such as w or $, to delete the text covered by that motion. Press d twice to delete the whole line. Uppercase D is a shortcut for d$ and immediately deletes from the cursor to the end of the line.
Conclusion
Use dd with a count or d with a linewise motion such as j to delete lines near the cursor. To edit text on matching lines while keeping the lines themselves, see how to find and replace in Vim
.
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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page