How to delete (not cut) in Vim?

VimCopy Paste

Vim Problem Overview


How can I delete a line without putting it into my default buffer?

Example:

line that will be copied.

line that I want to be substitued with the previous one.

What I'm trying to do:

yy
dd
p

But Vim replaces the recent copied string with the deleted (cutted) one. I know that I can use buffers like, "1yy, dd then "1p, but I always forget to put the copied string in a buffer then I need to paste my contents first (line that will be copied) and then delete what I want (line that I want to be substituted with the previous one.)

How can I really delete a text in Vi(m) without copying it?

Another related question is how I can forward delete a word in insert mode? I want something similar to Ctrl+w.

Vim Solutions


Solution 1 - Vim

The black hole register "_ will do the trick, but there is a better solution:

When you enter the line back with the p command you are pasting the contents of the (volatile) default register "", which has been overwritten by dd. But you still can paste from the (non volatile) yank register "0, which won't be overwritten by the delete command dd.

So these are the commands you want to use as per your example:

yy
dd
"0p

Solution 2 - Vim

Use the "black hole register", "_ to really delete something: "_d.
Use "_dP to paste something and keep it available for further pasting.

For the second question, you could use <C-o>dw. <C-o> is used to execute a normal command without leaving the insert mode.

You can setup your own mappings to save typing, of course. I have these:

nnoremap <leader>d "_d
xnoremap <leader>d "_d
xnoremap <leader>p "_dP

Solution 3 - Vim

That's one of the things I disliked about vim... I ended up mapping dd to the black hole register in my .vimrc and life has been good since:

nnoremap d "_d
vnoremap d "_d

Solution 4 - Vim

the following mappings will produce:

  • d => "delete"
  • leader d => "cut"

nnoremap x "_x
nnoremap d "_d
nnoremap D "_D
vnoremap d "_d

nnoremap <leader>d ""d
nnoremap <leader>D ""D
vnoremap <leader>d ""d

Also, it is a nice practice to have the "leader" key set to comma, e.g:

let mapleader = ","
let g:mapleader = ","

these 2 snippets will make ",d" be your new cut command.

If you would like to use these mappings togther with a shared system clipboard configuration, see further details at https://github.com/pazams/d-is-for-delete

Solution 5 - Vim

You can use "_d to prevent things from overwriting your yanked text. You can store yanked or deleted text in whatever register you want with ", and _ is the 'black hole' register, where you send stuff that you don't care about.

For more information you can type :help "_ or :help deleting

Solution 6 - Vim

I use noremap ' "_ in my .vimrc

This keep the behavior of dd, so I can use it to cut as before.

But when I really want to delete something, just use the prefix '. For example: 'dd,'dw

Solution 7 - Vim

I ended up with

nnoremap p "0p
nnoremap P "0P
vnoremap p "0p
vnoremap P "0P
vnoremap x "0x
nnoremap x "0x

Always paste from the 0 register instead of the unnamed one. Use x in visual mode to delete into the 0 register.

Solution 8 - Vim

yy
Vx
p

When in visual mode, x will delete the selection, so if you want to delete a whole line, first press V to select the line in visual mode and then press x to delete the selection.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaikoIDView Question on Stackoverflow
Solution 1 - VimcutemachineView Answer on Stackoverflow
Solution 2 - VimromainlView Answer on Stackoverflow
Solution 3 - VimkprobstView Answer on Stackoverflow
Solution 4 - VimpazamsView Answer on Stackoverflow
Solution 5 - VimAlexView Answer on Stackoverflow
Solution 6 - Vimqz3065863View Answer on Stackoverflow
Solution 7 - VimmowwwalkerView Answer on Stackoverflow
Solution 8 - VimAtemuView Answer on Stackoverflow