Learn Practice Daily golf Reference

How to delete to the end of a line in Vim

In Normal mode, press D to delete from the cursor to the end of the line. It is short for d$; C does the same and leaves you in Insert mode to type the replacement.

Try it here

Trim a comment, rewrite a value

The cursor is just before an old comment: D removes it. Then on the next line, go back to the quote and use C to replace the hard-coded URL with process.env.API_URL;.

1 of 2Delete the comment with D
1const retries = 3;
2const timeout = 5000; // was 3000, see #212
1const baseUrl = 'http://localhost:8080';
NORMALLoading the editor…1/20.0s2:22

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

KeysDeletes
D or d$from the cursor to the end of the line, including the character under the cursor
C or c$the same, then switches to Insert mode so you can type the new ending
2Dto the end of this line and the whole of the next one
d0from the start of the line up to the cursor (the character under the cursor stays)
d^back to the first non-blank character, so the indent stays
dt;up to, but not including, the next ; on the line
ddthe whole line, wherever the cursor is on it
"_Dto the end of the line, without copying it anywhere

Why D is d$

d is the delete operator and $ is the motion to the end of the line, so d$ reads "delete to the end". It is so common that it has its own key, D. The same pattern runs through Vim: C is c$ (change to the end) and d0 is delete to the start, with 0 the motion to column zero.

Like every delete, D cuts: the text goes into a register, and p pastes it back somewhere else. Use C when you want to replace the end of a line rather than just remove it: it saves you pressing i afterwards, and . repeats the whole change, the new text included, on another line.

The other direction

d0 deletes from the start of the line to the cursor, and d^ stops at the first non-blank character instead, which keeps the indentation in code. Both leave the character under the cursor alone, because a backward motion doesn't include the place it starts from. In Insert mode, Ctrl-u does a similar job: it deletes everything you typed before the cursor on the line.