Skip to content
Log In Sign Up info@muvcloud.com

Delete the Current Line

  1. Press Esc to return to normal mode.
  2. Move the cursor to the line.
  3. Press d twice: dd.

The line disappears and the lines below move up. Nothing is written to disk until you save. If you deleted the wrong line, press u; see how to undo and redo in Vim.

Delete Several Lines

KeysWhat it deletes
5ddThis line and the next four
djThis line and the next
dkThis line and the one above
dGTo the end of the file
dggTo the start of the file

Type the keys in normal mode. A number before dd is a count, so 5dd deletes five lines starting with the cursor line.

Delete Part of a Line

  • D: deletes from the cursor to the end of the line and leaves the line itself.
  • d0: deletes from the start of the line up to the cursor.
  • dw: deletes to the start of the next word.
  • x: deletes the character under the cursor.

Delete by Line Number

Commands that start with a colon work on line numbers. Turn the numbers on first with :set number, then type, and press Enter:

:5d
:5,10d
:.,$d
:.,+3d

:5d deletes line 5, and :5,10d lines 5 through 10. A dot means the current line and $ the last line, so :.,$d deletes from the cursor to the end. :.,+3d deletes the current line and the three after it.

Delete All Lines

:%d

% stands for every line in the file. In normal mode, ggdG does the same: gg jumps to the first line and dG deletes to the end. The buffer is now empty; save with :w to empty the file, or press u to get everything back. To quit without keeping the change, see how to save and quit in Vim.

Delete Lines Matching a Pattern

The global command runs a command on every line that matches:

:g/DEBUG/d
:g/^#/d
:g/^$/d
:g/^\s*$/d
:v/error/d
  • :g/DEBUG/d: deletes every line containing DEBUG.
  • :g/^#/d: deletes comment lines that start with #.
  • :g/^$/d: deletes empty lines; the next form also removes lines with only spaces or tabs.
  • :v/error/d: keeps only the lines that contain error and deletes all others.

The pattern is the same as in a search. Try it first with /DEBUG to see what matches; see how to search in Vim. One u undoes the whole global command.

Delete in Visual Mode

Press V to select the current line, move down with j or jump with G to extend the selection, then press d. This lets you see exactly what goes before you delete it.

Deleted Lines Are Cut, Not Lost

Vim keeps the deleted text in a register, so p pastes it below the cursor. ddp swaps the current line with the next one. If you want to delete without replacing what you copied earlier, use the black hole register: "_dd. Registers are explained in how to copy, cut, and paste in Vim. When editing a large log, jumping to the end of the file first saves time.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations