How to Delete Lines in Vim / Vi

By 

Updated on

7 min read

Vim: Delete Lines

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:

  1. Press Esc to make sure you are in normal mode.
  2. Place the cursor on the line you want to delete.
  3. 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:

  1. Press Esc to go to normal mode.
  2. Place the cursor on the first line you want to delete.
  3. 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:

  1. Press V (uppercase) to enter visual line mode.
  2. Use j or k to extend the selection up or down.
  3. Press d to delete the selected lines.

Deleting a Range of Lines

To delete a specific range, use the ex command :d with a line range:

vi
:[start],[end]d

For example, to delete lines 3 through 5:

  1. Press Esc to go to normal mode.
  2. Type :3,5d and 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:

vi
:'a,'bd

The 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 file
  • dgg - 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:

  1. Press Esc to go to normal mode.
  2. Type :%d and 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:

  • D or d$ - Delete from the cursor to the end of the line
  • d0 - Delete from the cursor to the beginning of the line
  • dw - Delete from the cursor to the start of the next word
  • db - 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:

vi
:g/pattern/d

To delete lines that do not match the pattern, add an exclamation mark:

vi
:g!/pattern/d

Vim also provides :v, which the documentation defines as being the same as :g!. The two are interchangeable, so use whichever you find clearer:

vi
:v/pattern/d

The 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:

vi
: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 .

CommandDescription
ddDelete the current line
5ddDelete 5 lines starting from the current line
djDelete the current line and the one below it
dkDelete the current line and the one above it
V then dSelect lines visually and delete
:3,5dDelete lines 3 through 5
:'a,'bdDelete from mark a to mark b
:.,$dDelete from current line to end of file
:1,.dDelete from beginning of file to current line
:%dDelete all lines
dGDelete from current line to end of file
dggDelete from current line to beginning of file
DDelete from cursor to end of line
d0Delete from cursor to beginning of line
:g/pattern/dDelete all lines matching a pattern
:g!/pattern/dDelete all lines not matching a pattern
:v/pattern/dShorthand for :g!/pattern/d
:g/pattern/d _Delete matching lines without touching the registers
uUndo 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

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