1

I need to re-factor a line with a repeated pattern in a way to apply some math over numbers. For e.g. let's assume the following line of code:

testpoints = [Point(1,2), Point(1,1), Point(1,2), Point(0,2), Point(2,2), Point(3,3), Point(1,3), Point(1,4), Point(2,3), Point(2,0), Point(2,1),]

and some function should be applied to all matches for e.g. (x, y) -> (x*10, y*10).

testpoints = [Point(10,20), Point(10,10), Point(10,20), Point(0,20), Point(20,20), Point(30,30), Point(10,30), Point(10,40), Point(20,30), Point(20,0), Point(20,10),]

The obvious solution is to add '*10' after both arguments:

:s/\(\d\+\)\(,\s*\)\(\d\+\)/\1*10\2\3*10/g

But, probably it will be better to do the math in the VIM. I tried with :g but with no success:

:g/(\s*\d\+\s*,/normal 10<cr>af,w10<cr>a

The idea was to find the proper ( and then to do 10 Ctrl+A , then move to next number with w and again to increment ten times ...

Let me wrap up:

  1. Is possible to be done this with :g command ?
  2. Is there another more vim-native way for such substitution with math?
4

See sub-replace-expression and :help submatch():

:s/\(\d\+\)/\=submatch(1)*10/g

FWIW, the :global command is completely unrelated to the problem at hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.