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.

  • Tok

    Tok Correct answer

    10 years ago

    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=|` with `|` being the cursor location. Is there a command that will delete all the ``?

    @Falmarri - I apologize, I must have misunderstood your question. There is a command for that: `d$`

    D (Shift+d) is also a synonym for d$.

    `dd` also does the job. It deletes the whole line.

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

    @Tok How about from the beginning of the line till the current cursor?

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

    The answer here is usually more practical for my use cases, it deletes end of line and puts into insert mode, thus not putting you on the last character of the line.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM