Is there a more efficient way to replace text in vim using the visual command?

  • Thread starter Thread starter DrDu
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 3K views
Messages
6,423
Reaction score
1,004
I have a recurrent vim problem:
Often, I want to replace part of a file (call it B) with text from some other file (A).
So in A I select the lines in question and yank them.

Now in B i use the visual command to select the region I want to replace.
I am not happy with it for two reasons:
1. I would prefer to specify the region explicitly, something like: 1,20v
however this seems not to be possible with the visual command.
2. the visual command replaces the text in the yank buffer with the text deleted upon paste.
If I want to repeat the operation in another file C, I have to yank the text in file A again.

There must be some more elegant way, however I didn't find it.
 
Physics news on Phys.org
Try openng each of the paste files from your yank file. It might depend on your OS (I used slackware last time I was messing with this) but that seemes ti work for me.
 
Ok, thank you, I will give this a try. However, I often copy from outside, i.e. form the clipboard, e.g. from a pdf, so this won't work always.
 
If you are able to paste into one vim instance from outside, you might be able to open documents within that vim instance and continue to paste (I always have annoying issues trying to paste from outside, so I don't know). Can always yank it right after you paste it in the first file, too. Often, getting it to actually paste into the first term is where I always have issues (slackware 14.0).
 
I just realized a slightly easier way to do this. Instead of :open, use :e it allows you to auto-complete with TAB as you dig into directories.