Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pasting inside delimiters without using visual selection

Tags:

vim

In Vim, let's say I want to replace the contents of one String with the content of another.

Original input

var1 = "January"
var2 = "February"

Desired output

var1 = "January"
var2 = "January"

What I would usually do is:

  • Move cursor to line 1
  • y i " (yank inner quotes)
  • Move cursor to the destination quote in line 2
  • v i " p (visual select inner quotes, paste)

While this works well, I generally try to avoid visual mode when possible, so I am not completely satisfied with my step 4 (v i " p).

Is there any way to specify a "destination paste area" without using Visual mode? I suspect it might be something chained to g, but I can't think of anything.

like image 259
krystah Avatar asked May 08 '26 13:05

krystah


1 Answers

There are many ways of doing this however using visual mode is the easiest.

  • Use the black hole register to delete the content then paste. e.g. "_di"P
  • Do ci"<c-r>0. <c-r> inserts the contents of a register
  • Simply paste and then move over a character and delete the old text. e.g pldt"

However visual mode still has my vote. I find that the concerns most people have is that using visual mode + paste is that the default register is swap with the selected text and it doesn't repeat well. Good news everybody! The 0 register always stores the last yank. The bad news is visual mode still doesn't repeat well. Take a look at this vimcast episode, Pasting from Visual mode, for more information. It mentions a few plugin that help with this.

like image 121
Peter Rincker Avatar answered May 10 '26 19:05

Peter Rincker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!