Lately I've been y
anking and p
utting a lot of code that needs to be altered somewhat (usually just a simple substitution). I can manually select it after it's pasted in, or for longer blocks I suppose I could look at the number of lines pasted (20 new lines
) and use 20:s...
, but given that it's vim, it seems like there should be an easier/faster way to do this.
So is there a way to either select or execute a substitution on text as it's being p
ut?
Immediately after the execution of the p
, the [
and ]
marks refer to the start and end line numbers of the pasted region (applies during y
as well). See the help for '[
and ']
for explanation.
Thus, you can use these marks to form the range on which to work the :s
, as :'[,']s///
. This will then work on the region just yanked or pasted. Sure, it's not short, but if you care about it you can map it. Perhaps something like nnoremap <Leader>p p:'[,']s/
.
Chris Morgan already posted the best solution. But you can also do some kinds of manipulation directly on the text in the register. The default register for yank is "
, so you can do something like:
:let @" = substitute(@",'someword', 'somedifferentword','g')
Then paste the altered register text. Not as easy for manipulation related to line-context in multi-line register text, since the text in register is a single string with one begin pattern (^
) and one end ($
). But still can be useful.
Just thought I'd throw it out there, since this is something I sometimes do.
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