How to Delete Lines in Vim / Vi

By 

Updated on

5 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.

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

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

  • 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

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.

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
V then dSelect lines visually and delete
:3,5dDelete lines 3 through 5
:.,$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
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.

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