vim将所有regex组的比赛都拉入寄存器

时间:2021-04-27 19:18:08

I know that I can yank all matched lines into register A like this:

我知道我可以像这样把所有匹配的线拉进寄存器A:

:g/regex/y/A

But I can't seem to figure out how to yank match regex groups into register A:

但我似乎不知道如何将regex组与regex组匹配为:

:g/\(regex\)/\1y A
(E10: \ should be followed by /, ? or &)

3 个解决方案

#1


11  

You can do this with a substitute command.

您可以使用替换命令来实现这一点。

:%s/regex/\=setreg('A', submatch(0))/n

This will append register a to whatever the regex matched. The n flag will run the command in a sandbox so nothing will actually get replaced but the side effects of the statement will happen.

这将向regex匹配的任何内容追加寄存器a。n标志将在沙箱中运行命令,因此实际上不会替换任何东西,但语句的副作用将会发生。

You probably want to empty the register first with

您可能希望首先清空寄存器

:let @a=''

#2


0  

If you just want to grab one part of the match, you can work with \zs and \ze. You need capture groups only for multiple parts, or reordering.

如果你只想抓取比赛的一部分,你可以使用\zs和\ze。您只需要多个部分的捕获组,或者重新排序。

My ExtractMatches plugin provides (among others) a convenient :YankMatches command that also supports replacements:

我的extractmatch插件提供了一个方便的:YankMatches命令,它也支持替换:

:[range]YankMatches[!] /{pattern}/{replacement}/[x]

#3


0  

You can also yank all matched line between two sessions to pointed register.

您还可以将两个会话之间的所有匹配行拖拽为指向寄存器。

By way of example:

举个例子:

:11,21s/regex/\=setreg('A', submatch(0))/n

Matches regrex group from line 11 to line 21 rather than the whole file.

从第11行匹配到第21行,而不是匹配整个文件。

:/^ab/,/^cd/s/regex/\=setreg('A', submatch(0))/n

Matches regrex group from the line that stards with ab to line with cd.

用ab与cd线匹配的线上的回归组。

More about session: http://vimregex.com/

更多关于会话:http://vimregex.com/

#1


11  

You can do this with a substitute command.

您可以使用替换命令来实现这一点。

:%s/regex/\=setreg('A', submatch(0))/n

This will append register a to whatever the regex matched. The n flag will run the command in a sandbox so nothing will actually get replaced but the side effects of the statement will happen.

这将向regex匹配的任何内容追加寄存器a。n标志将在沙箱中运行命令,因此实际上不会替换任何东西,但语句的副作用将会发生。

You probably want to empty the register first with

您可能希望首先清空寄存器

:let @a=''

#2


0  

If you just want to grab one part of the match, you can work with \zs and \ze. You need capture groups only for multiple parts, or reordering.

如果你只想抓取比赛的一部分,你可以使用\zs和\ze。您只需要多个部分的捕获组,或者重新排序。

My ExtractMatches plugin provides (among others) a convenient :YankMatches command that also supports replacements:

我的extractmatch插件提供了一个方便的:YankMatches命令,它也支持替换:

:[range]YankMatches[!] /{pattern}/{replacement}/[x]

#3


0  

You can also yank all matched line between two sessions to pointed register.

您还可以将两个会话之间的所有匹配行拖拽为指向寄存器。

By way of example:

举个例子:

:11,21s/regex/\=setreg('A', submatch(0))/n

Matches regrex group from line 11 to line 21 rather than the whole file.

从第11行匹配到第21行,而不是匹配整个文件。

:/^ab/,/^cd/s/regex/\=setreg('A', submatch(0))/n

Matches regrex group from the line that stards with ab to line with cd.

用ab与cd线匹配的线上的回归组。

More about session: http://vimregex.com/

更多关于会话:http://vimregex.com/