How to delete lines in Vim

In the Vim editor, we can delete lines with the dd command.

Delete a single line

Follow these steps to delete a single line:

  • Press ESC to go to Normal mode.

  • Place the cursor on the line you need to delete.

  • Press dd. This will delete the current line.

If you press dd multiple times, it will delete multiple lines.

Delete a specific line

Follow these steps to delete a specific line:

  • Press Esc then : to go to command mode.

  • Enter nd and hit Enter where n in nd is the line number. For example, if you want to delete the fifth line, then you should use 5d.

Delete multiple lines

Syntax

:[start],[end]d

To delete line numbers 5 to 10, use:

:5,10d

We can also use certain characters to specify a range, such as:

  • . (dot) specifies the current line.
  • $ specifies the last line.
  • % specifies all lines.

To delete all lines below the current line, use the following command.

:.,$d

The . in the above code denotes the current line, and the $ denotes the last line. This command will delete all the lines below the current line.

To delete all the lines above the current line, use the following command.

:.,1d

To delete all the lines of the file, use the following command.

:%d

Free Resources