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?
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”.
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.
To clear the a register, for instance, I type q a q to set the a register to an empty string.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With