Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim is there a way to delete without putting text in the register?

Using Vim I often want to replace a block of code with a block that I just yanked.

But when I delete the block of code that is to be replaced, that block itself goes into the register which erases the block I just yanked. So I've got in the habit of yanking, then inserting, then deleting what I didn't want, but with large blocks of code this gets messy trying to keep the inserted block and the block to delete separate.

So what is the slickest and quickest way to replace text in Vim?

  • is there a way to delete text without putting it into the register?
  • is there a way to say e.g. "replace next word" or "replace up to next paragraph"
  • or is the best way to somehow use the multi-register feature?
like image 513
Edward Tanguay Avatar asked Sep 10 '08 14:09

Edward Tanguay


People also ask

How do I delete part of a line in Vim?

Deleting a single line in Vim editor: To delete a line, follow the steps below: First, bring your cursor to the line you want to delete. Press the “Esc” key to change the mode. Now type, “:d”, and press “Enter” to delete the line or quickly press “dd”.

How do I delete a block of text in Vim?

Step 1: Move your cursor to the top line of the block of text/code to remove. Step 2: Press V (Shift + v) to enter the Visual mode in Vim/Vi. Step 3: Now move your cursor down to the bottom of the block of text/code to remove. Step 4: Press d to delete the block selected.

How do I delete a register in Vim?

To clear the a register, for instance, I type q a q to set the a register to an empty string.


2 Answers

To delete something without saving it in a register, you can use the "black hole register":

"_d 

Of course you could also use any of the other registers that don't hold anything you are interested in.

like image 111
Christian Berg Avatar answered Nov 17 '22 10:11

Christian Berg


Yep. It's slightly more convoluted than deleting the "old" text first, but:

I start off with..

line1 line2 line3 line4  old1 old2 old3 old4 

I shift+v select the line1, line 2, 3 and 4, and delete them with the d command

Then I delete the old 1-4 lines the same way.

Then, do

"2p 

That'll paste the second-last yanked lines (line 1-4). "3p will do the third-from-last, and so on..

So I end up with

line1 line2 line3 line4 

Reference: Vim documentation on numbered register

like image 28
dbr Avatar answered Nov 17 '22 11:11

dbr