Vi: is there a way to manipulate regular expressions?

I have a file with the following line:

img width="240" height="120"

I want to scale the width and height by the same amount so that

:%s/width="\\(.*\\)" height="\\(.*\\)"/width="2*\\1" height="2*\\2"/g

produces

img width="2*240" height="2*120"

anyway, to get vi to actually compute 2*240=480and put 480into the result.

thank you for your help.

+3
source share
2 answers

I can get something close to what you ask with

:s/\(\d\+\)/\=submatch(1)*2/gc

But I would use an external filter.

+4
source

I would usually use an external filter for this kind of thing:

:%! perl -pe 's / width = "(\ d *)" / sprintf "width = \" \% d \ "", 2 * $ 1 / e'

, , perl. % vim .

:

:help sub-replace-expression

.

+1

Source: https://habr.com/ru/post/1733265/


All Articles