Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim alter text as or immediately after it's put

Lately I've been yanking and putting 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 put?

like image 662
Kevin Avatar asked Nov 23 '11 16:11

Kevin


2 Answers

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/.

like image 132
Chris Morgan Avatar answered Sep 21 '22 16:09

Chris Morgan


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.

like image 25
Herbert Sitz Avatar answered Sep 21 '22 16:09

Herbert Sitz