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

  • Thread starter DrDu
  • Start date
In summary, the problem is that the visual command replaces the text in the yank buffer with the text deleted upon paste.
  • #1
DrDu
Science Advisor
6,375
980
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
  • #2
When you open B and C, are you opening them from within A like:

:eek:pen (B)
:eek:pen (C)

?
 
  • #3
No, usually, I use different windows.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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).
 
  • #7
I just realized a slightly easier way to do this. Instead of :eek:pen, use :e it allows you to auto-complete with TAB as you dig into directories.
 

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

What is "paste with overwrite" in Vim?

"Paste with overwrite" is a feature in Vim that allows you to paste text in a way that replaces the existing text instead of inserting it. This can be useful when you want to replace a block of text with new text without having to manually delete the old text first.

How do I use "paste with overwrite" in Vim?

To use "paste with overwrite" in Vim, you can either use the p command in normal mode followed by the o command, or use the P command in normal mode followed by the O command. You can also use the put command in normal mode with the ! flag to paste with overwrite.

Can I paste with overwrite multiple times in Vim?

Yes, you can paste with overwrite multiple times in Vim. Simply use the p or P command multiple times in normal mode, or use the put command with the ! flag multiple times.

Can I undo "paste with overwrite" in Vim?

Yes, you can undo "paste with overwrite" in Vim by using the u command in normal mode. This will undo the last change you made, including the paste with overwrite. You can also use the U command to undo all changes made on the current line.

Are there any alternatives to "paste with overwrite" in Vim?

Yes, there are a few alternatives to "paste with overwrite" in Vim. You can use the d command in visual mode to delete the selected text and then use the p command to paste the new text. You can also use the c command in visual mode to replace the selected text with new text. Lastly, you can use the r command in normal mode to replace a single character with a new character.

Similar threads

Replies
4
Views
3K
Replies
3
Views
811
Replies
3
Views
4K
Replies
7
Views
2K
Replies
2
Views
3K
Back
Top