Delete from cursor to end of line in `vi`

I know I’ve probably looked over this a million times in all the vi documents I’ve read, but I can’t seem to find the delete from cursor to end of line command.

Asked By: Falmarri

||

D (uppercase letter D)

The command dw will delete from the current cursor position to the beginning of the next word character. The command d$ (note, that’s a dollar sign, not an ‘S’) will delete from the current cursor position to the end of the current line. D (uppercase D) is a synonym for d$ (lowercase D + dollar sign).

Answered By: Tok

One of the nice things about vi is its logical command structure. d followed by a motion command deletes to the target of that motion. $ moves to the end of the line (mnemonic: like in regexps). So d$ deletes to the end of the line. Similarly, e moves to the end of the current word, and w moves to the beginning of the next word; so de deletes the end of the current word, and dw additionally deletes the following whitespace.

You probably want to use D. Move the cursor to the first character you want to delete, then hit shift-D. Everything gone. Actually, it’s in the default cut buffer, so you can P or p paste it back in.

I use Dp (delete to end of line, then put it back), move to the end of some other line, then p again to paste the same text in at the end of this other line. Works wonders in config files, where you need to put some complicated URL in two or more places.

Answered By: user732

As others have mentioned: you can use d$ or D (shiftd) to delete from the cursor position until the end of the line.

What I typically find more useful is c$ or C (shiftc) because it will delete from the cursor position until the end of the line and put you in [INSERT] mode.

Answered By: Brad Johnson

I think an insert mode shortcut could come in handy.

In insert mode maybe would be better starting changing until the end of line (put this on your ~/.vimrc):

inoremap <C-l> <C-o>C

So you have, as have been said, D in normal mode and Ctrl+l in insert mode. As you can see you have also C that starts changing till the end of the line.

<C-o> ......... insert normal keystroke in insert mode

I have chosen Ctrll because l is under your fingers. The Ctrlk is already used to insert digraphs.

I have been searching through :h i_Ctrl for some free keybindings, and it is actually the bigger problem when it comes to creating new shortcuts to perform actions in vim.

Answered By: SergioAraujo

To delete a range of lines from after the cursor position, 3D will delete from the cursor until the end of the line, as well as the next two lines completely (i.e., deletes 3 lines after the cursor position).

e.g. for the following text (cursor represented as |),

If there's a cursor |in the line
here
we
go

Using the command 3D will output:

If there's a cursor
go
Answered By: Nick Bull
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.