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.
-
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 =]– RonnyOct 17, 2022 at 14:13
6 Answers
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).
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.
-
11NICE!!! 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. Mar 21, 2017 at 23:17 -
2Why is this answer hidden all the way down here? This is the functionality I was really looking for when I came here.– M_MJul 13, 2017 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, anda
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 usea
.– Lie RyanOct 22, 2018 at 13:30 -
3The goal is to do it in one operation, not two. Extra key strokes = extra friction. Jan 10, 2019 at 1:42
-
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>
, butd$
. Apr 29, 2014 at 9:42 -
@Gilles You are right. I've just tried d^ and it deleted from cursor position to the line begin.– ka3akApr 2, 2016 at 11:57
-
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.
-
-
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 asy$
but one key press would be nice.– nevelisOct 12 at 19:51
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.
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