Learn Practice Daily golf Reference

How to delete a line in Vim

In Normal mode, put the cursor anywhere on the line and press dd. A count deletes more: 3dd deletes this line and the two below it.

Try it here

Take out the debug lines

Two console.log lines were left behind. The cursor is on the first one: dd it, then move down to the second and do the same.

1 of 2Delete this line with dd
1function total(items) {
2 console.log('items', items);
1 let sum = 0;
2 console.log('start');
3 for (const i of items) sum += i.price;
4 return sum;
5}
NORMALLoading the editor…1/20.0s2:1

This editor needs a keyboard. Read on, and play it at your computer.

KeysDeletes
ddthe line the cursor is on
3ddthree lines, starting with this one
dj / dkthis line and the one below / above
dGfrom this line to the end of the file
dggfrom this line to the top of the file
d}to the next blank line (the rest of the paragraph)
:d ↵the current line, from the command line
:5,10d ↵lines 5 to 10
:g/console/d ↵every line that contains console
"_ddthe line, without copying it anywhere (see below)
Before
function total(items) {
console.log('items', items);
let sum = 0;
console.log('start');
for (const i of items) sum += i.price;
return sum;
}
:g/console/d↵
function total(items) {
let sum = 0;
for (const i of items) sum += i.price;
return sum;
}
:g/console/d deletes every line that mentions console, wherever the cursor is

Deleting is cutting

A deleted line isn't gone: Vim puts it in a register, the way a cut puts text on a clipboard. p puts it back below the cursor and P above, so dd then p moves a line down one, and dd somewhere else then p moves it anywhere.

That's handy until you delete a line and then want to paste something you copied earlier: the delete replaced it. Delete into the "black hole" register instead, "_dd, and your copy stays where it was. (Your earlier copy is also still in register 0: "0p.)