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

  • Thread starter Thread starter DrDu
  • Start date Start date
AI Thread Summary
The discussion revolves around efficiently replacing text in a file using Vim, specifically addressing two main issues. First, users express a desire to explicitly specify the text region for replacement rather than relying on the visual command, which does not allow for precise range selection. Second, the visual command replaces the yank buffer with the deleted text, necessitating re-yanking when performing the same operation across multiple files. Suggestions include opening files within the same Vim instance to maintain the yank buffer, although challenges with pasting from external sources like PDFs are noted. An alternative method using the ":e" command for file opening is also mentioned, as it allows for easier navigation and auto-completion. Overall, the conversation highlights the need for a more streamlined process for text replacement in Vim.
DrDu
Science Advisor
Messages
6,421
Reaction score
1,003
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
When you open B and C, are you opening them from within A like:

:open (B)
:open (C)

?
 
No, usually, I use different windows.
 
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.
 
Back
Top