459

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.

0xSheepdog
  • 2,750
Falmarri
  • 13,047
  • I would suggest to play around with vimtutor, delete to end, words, number of words, lines... and more of course always good to do a refresher =] – Ronny Oct 17 '22 at 14:13

6 Answers6

671

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).

ilkkachu
  • 138,973
Tok
  • 10,744
128

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

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

Brad Johnson
  • 1,381
  • 11
    NICE!!! This is much better than D for most people, in my opinion, as it doesn't then move you back a space at the end of the line. – Elijah Lynn Mar 21 '17 at 23:17
  • 2
    Why is this answer hidden all the way down here? This is the functionality I was really looking for when I came here. – M_M Jul 13 '17 at 15:07
  • 1
    @ElijahLynn it should not be an issue anyway. Vim has two main ways to enter insert mode i which inserts before the cursor, and a which inserts after the cursor. If your cursor is at the end of the line and you want to insert at the end of the line, you can just use a. – Lie Ryan Oct 22 '18 at 13:30
  • 3
    The goal is to do it in one operation, not two. Extra key strokes = extra friction. – Elijah Lynn Jan 10 '19 at 01:42
  • I don't know why I didn't use this before. C is in most cases what you need. – COil Apr 08 '23 at 10:03
43

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.

23

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.

  • It's probably a minimal Vim he uses anyway. – polemon Dec 03 '10 at 09:17
  • 1
    Nice - I use C-esc-p because I didn't know about D :) so I learned something today. What I'd like is one key-press to copy to the end of the line without cutting or having to paste it back. Is there a command for this? Same behavior as y$ but one key press would be nice. – nevelis Oct 12 '23 at 19:51
3

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 Ctrl-l because l is under your fingers. The Ctrl-k 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.

SergioAraujo
  • 459
  • 6
  • 8
2

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
Nick Bull
  • 563