306

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.

  • 3
    You've got a typo here - you say "end of line" in the title, but "end of word" in the question. – Cascabel Nov 30 '10 at 17:50
453

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 is a synonym for d$.

  • Yeah I know of dw, but I guess I just assumed there would be a "delete the rest of this line" command. For example my line has ip=|10.1.1.1 with | being the cursor location. Is there a command that will delete all the 10.1.1.1? – Falmarri Nov 29 '10 at 21:03
  • 3
    @Falmarri - I apologize, I must have misunderstood your question. There is a command for that: d$ – Tok Nov 29 '10 at 21:04
  • 84
    D (Shift+d) is also a synonym for d$. – Neth Nov 29 '10 at 22:06
  • 5
    @Andy, the job is to delete from the cursor to the end of the line. If you consider deleting more than that to be "doing the job," you could also just use :!rm -f % and then :q!. That deletes the whole file. – Wildcard Oct 27 '17 at 1:28
  • 2
    @Amir Just use d^ ('d' + caret)...basically d + any character that would normally move the cursor I think, but I am learning Vim myself, so I could be wrong on that. – JakeD Dec 24 '17 at 2:25
43

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.

  • 2
    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
  • 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
  • @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
  • 1
    The goal is to do it in one operation, not two. Extra key strokes = extra friction. – Elijah Lynn Jan 10 at 1:42
36

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.

  • 1
    @gioele I use <kbd> for actual keys, not for command sequences. E.g. <kbd>Ctrl</kbd>+<kbd>C</kbd>, but d$. – Gilles Apr 29 '14 at 9:42
  • @Gilles You are right. I've just tried d^ and it deleted from cursor position to the line begin. – ka3ak Apr 2 '16 at 11:57
  • I've always used (d)elete (a) (w)ord for deleting words :) – Błażej Michalik Sep 21 '16 at 20:51
20

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 3 '10 at 9:17
0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.