简体   繁体   English

submatch 命令在 Vim 中如何工作?

[英]How Does the submatch Command Work In Vim?

I came across this really nifty and handy command我遇到了这个非常漂亮和方便的命令

:.,$/^\d\+/\=submatch(0) + 1/g

What this does is it finds any line after the current line, and checks if that line begins with a number.它的作用是查找当前行之后的任何行,并检查该行是否以数字开头。 If so, it will replace it with the increment of its current value;如果是这样,它将用其当前值的增量替换它; however, I'm quite confused with how this is actually happening.但是,我对这实际上是如何发生的感到很困惑。 I am guessing that the \=submatch(0) portion is what is doing the heavy lifting, but I cant figure out how.我猜\=submatch(0)部分是做繁重的工作,但我不知道怎么做。 There is also a + 1 after the submatch which is acting as arithmetic, which is surprising to me.在作为算术的submatch之后还有一个+ 1 ,这让我感到惊讶。 I would have thought that it would be inserted as text instead of performing arithmetic on the matched number.我原以为它将作为文本插入,而不是对匹配的数字执行算术。

Since the string starts with a \= , it is evaluated as an expression .由于字符串以\=开头,因此它被评估为expression In this case, submatch(0) + 1 is evaluated as a function .在这种情况下, submatch(0) + 1被评估为function

submatch : submatch

The whole matched text can be accessed with " submatch(0) ".可以使用“ submatch(0) ”访问整个匹配的文本。 The text matched with the first pair of () with " submatch(1) ".与第一对()匹配的文本带有“ submatch(1) ”。 Likewise for further sub-matches in () .对于()中的进一步子匹配也是如此。

In the regex above, the whole match is actually the digits (the first number on each matched line).在上面的正则表达式中,整个匹配实际上是数字(每个匹配行上的第一个数字)。 So submatch(0) + 1 will add 1 to the captured match.所以submatch(0) + 1会将 1 添加到捕获的匹配中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM