Delete the Current Line
- Press
Escto return to normal mode. - Move the cursor to the line.
- Press
dtwice: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
| Keys | What it deletes |
|---|---|
5dd | This line and the next four |
dj | This line and the next |
dk | This line and the one above |
dG | To the end of the file |
dgg | To 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.