Vim搜索替换正则表达式+增量函数 [英] Vim search replace regex + incremental function
问题描述
目前,我一直在vim中试图找到一个搜索/替换oneliner,以用另一个+每个新迭代的增量替换一个数字=当它找到一个新的匹配时。
< tspan
x =938.91315
y =783.20563
id =tspan13017
style =font-weight:bold> Text1:< tspan>< tspan
x =938.91315
y =833.20563
id =tspan13019> Text2< / tspan>< tspan
x =938.91315
y =883.20563
id =tspan13021>✗Text3< / tspan>
等。
要做的就是改变这个结果:
< tspan
x =938.91315
y = 200
id =tspan13017
style =font-weight:bold> Text1:< / tspan>< tspan
x =938.91315 240
id =tspan13019> Text2< / tspan>< tspan
x =938.91315
y =280
id =tspan13021>文本3< / TSPAN>
等。
从zzapper中发现了最好的vim tips资源,但我无法理解它:
将yy转换为10,11,12:
:let i = 10 | 'a,'bg / Abc / s / yy / \ = i / | let i = i + 1
它的东西,我可以理解,应该在我的家庭VIM工作:
:let i = 300 | 327,$ smagic! y = \[0-9] \ +。[0-9] \ + \! \ =我! g |让我=我+ 50
但不知何故,它不循环,我得到的是:
< tspan
x =938.91315
300
id =tspan13017
style =font-weight:bold> Text1:< / tspan>< tspan
x =938.91315
300
id =tspan13019> tspan>< tspan
x =938.91315
300
id =tspan13021>✗Text3< / tspan>
所以我在这里被严重卡住了。我找不出什么不起作用:
- 我改编了原始公式?
- 我的数据布局?
- 我的.vimrc?
我会尝试查找其他资源我自己,但在这种技巧,他们是非常罕见的,我发现,和zzapper技巧一样,并不总是与手册一起交付。 解决方案
$ p
$ b
:let i = 300 | g / \ m \< y = / s / \my =\zs\d\ + .\d\ + \ze/ \ = i / |让我+ = 50
翻译:
-
let i = 300
- 希望很明显 -
g / \m \< y = / ...
- 对于所有匹配\m\
- 一位或多位数字的行,应用以下命令; 以下命令是 s /.../.../ |让...
;正则表达式:
-
\m
- magicregexp
\<
- 仅在字边界匹配
-
s / \my =\zs\d\ + .\d\ + \ze/ \ = i /
- substitute;正则表达式:
-
\m
- magicregexp
\ d\ +
-
-
-
\ zs ... \ze
- 仅替换这些点之间匹配的内容 -
\ = i
- replace with表达式的值i
-
let i +
$ b
更多信息::help: g
,:help \ zs
,:help \ze
, help s / \\ =
。
I'm currently stuck in vim trying to find a search/replace oneliner to replace a number with another + increment for each new iteration = when it finds a new match.
I'm working in xml svg code to batch process files Inkscape cannot process the text (plain svg multiline text bug).
<tspan
x="938.91315"
y="783.20563"
id="tspan13017"
style="font-weight:bold">Text1:</tspan><tspan
x="938.91315"
y="833.20563"
id="tspan13019">Text2</tspan><tspan
x="938.91315"
y="883.20563"
id="tspan13021">✗Text3</tspan>
etc.
So what I want to do is to change that to this result:
<tspan
x="938.91315"
y="200"
id="tspan13017"
style="font-weight:bold">Text1:</tspan><tspan
x="938.91315"
y="240"
id="tspan13019">Text2</tspan><tspan
x="938.91315"
y="280"
id="tspan13021">✗Text3</tspan>
etc.
So I duckducked and found the best vim tips resource from zzapper, but I cannot understand it:
convert yy to 10,11,12 :
:let i=10 | ’a,’bg/Abc/s/yy/\=i/ |let i=i+1
I then adapted it to something I can understand and should work in my home vim:
:let i=300 | 327,$ smagic ! y=\"[0-9]\+.[0-9]\+\" ! \=i ! g | let i=i+50
But somehow it doesn't loop, all I get is that:
<tspan
x="938.91315"
300
id="tspan13017"
style="font-weight:bold">Text1:</tspan><tspan
x="938.91315"
300
id="tspan13019">Text2</tspan><tspan
x="938.91315"
300
id="tspan13021">✗Text3</tspan>
So here I'm seriously stuck. I cannot figure out what doesn't work :
- My adaptation of the original formula ?
- My data layout ?
- My .vimrc ?
I'll try to find other resources by myself, but on that kind of trick they are pretty rare I find, and like in zzapper tips, not always delivered with a manual.
One way to fix it:
:let i = 300 | g/\m\<y=/ s/\my="\zs\d\+.\d\+\ze"/\=i/ | let i += 50
Translation:
let i = 300
- hopefully obviousg/\m\<y=/ ...
- for all lines matching\m\<y=
, apply the following command; the "following command" iss/.../.../ | let ...
; the regexp:\m
- "magic" regexp\<
- match only at word boundary
s/\my="\zs\d\+.\d\+\ze"/\=i/
- substitute; the regexp:\m
- "magic" regexp\d\+
- one or more digits\zs...\ze
- replace only what is matched between these points\=i
- replace with the value of expressioni
let i += 50
- hopefully obvious again.
For more information: :help :g
, :help \zs
, :help \ze
, help s/\\=
.
这篇关于Vim搜索替换正则表达式+增量函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!