I'm trying to search & replace inside my file for all occurrences of a particular pattern, then call toupper() on each match. In practice, there aren't that many occurrences that I couldn't just do this by hand, but I'm curious to improve my Vim skills and find a (simple) way to get Vim to do this for me. I say simple, otherwise I'll just forget.
From a read over the Vim help for s/, I should be able to do something like this:
:%s/\vfunction ([a-z0-9_]+)/\="function " . toupper("\1")/g
(Assuming I'm uppercasing all function definition names).
This actually just replaces the function names with the control character ^A though. How do I refer to the capturing group in the regexp?
Apologies. I should read more closely. I'll answer my own question anyway, instead of deleting it. You need to use submatch(n) to refer to the capturing group:
:%s/\vfunction ([a-z0-9_]+)/\="function " . toupper(submatch(1))/g
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