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