VIM替换:使用当前行作为搜索字符串

发布于 2024-08-10 19:48:20 字数 354 浏览 4 评论 0原文

假设以下文本文件,实际上是资金和价格统计数据的数据转储:

PBCPDF
05/01/2006
 0.0000
 0.0000
PBCPDF
 0.0000
06/01/2006
 0.0000
 0.0000
PBCPDF
 0.0082
[… lines repeat …]

我想要实现的是删除除第一个之外的所有 PBCPDF 实例,我可以编写替换命令如 :.+1,$s/PBCPDF\n//g

但是,由于我想编写一个宏来处理多个基金名称,因此我需要一种方法来使用某种模式来检索当前行作为搜索模式,而无需我手动执行。

有办法做到这一点吗?

Assuming the following text file, which is actually a data dump of funds and price statistics:

PBCPDF
05/01/2006
 0.0000
 0.0000
PBCPDF
 0.0000
06/01/2006
 0.0000
 0.0000
PBCPDF
 0.0082
[… lines repeat …]

What I wanted to achieve is to delete all instances of PBCPDF except for the first one, which I could write the substitution command as :.+1,$s/PBCPDF\n//g.

However, since I wanted to program a macro to process multiple fund names, I need a means to use some sort of pattern that would retrieve the current line as the search pattern without me doing it by hand.

Is there a way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间☆小暴躁 2024-08-17 19:48:20
ggyy:+1,$s/<Ctrl-R>"<BS>//g

让我们看看它到底做了什么。

转到第一行

gg

复制整行

yy

开始替换

:+1,$s/

从您复制到的寄存器中获取当前行:

<Ctrl-R>"

注意:不要键入 ",您必须实际按住 Control 并按 R,后跟双引号删除

行尾字符:

<BS>

注意:按 Backspace 键

替换任何内容,

//g

您可以用 qq/q 或其他字符将其记录在宏中。

ggyy:+1,$s/<Ctrl-R>"<BS>//g

Let's see what that does, exactly.

Go to the first line

gg

Yank the entire liine

yy

Start the substitution

:+1,$s/

Get the current line from the register to which you yanked it to:

<Ctrl-R>"

Note: Don't type the ", you have to actually hold Control and press R, followed by a double quote.

Delete the end of line character:

<BS>

Note: press Backspace.

Replace with nothing.

//g

You can record this in a macro by wrapping it with a qq/q, or whatever.

二货你真萌 2024-08-17 19:48:20

如果您在宏中执行此操作(使用 q 命令),那么当您执行一些更深奥的击键命令时,它足够聪明,可以捕获 - 我特别想到的是 CTRL-R、CTRL-W,这将将光标下的单词插入命令行中。因此,举例来说,如果您这样做了

ESC
qw
/[CTRL-R, CTRL-W] (you should see the word under the cursor enter the command line)
[enter]
q

,那么您刚刚创建了一个宏来搜索缓冲区中与此相同的下一个单词,明白了吗?它将动态地将光标下的单词作为宏的一部分拉入命令中。上面的命令中可能有像 CTRL-W 之类的东西可以对整行执行此操作,但我不知道。

If you're doing this in a macro (using the q command) it's smart enough to catch when you do some of the more esoteric keystroke commands - the one I'm specifically thinking about is CTRL-R, CTRL-W, which will insert the word under the cursor into the command line. So, for example, if you did

ESC
qw
/[CTRL-R, CTRL-W] (you should see the word under the cursor enter the command line)
[enter]
q

then you just created a macro that searches for the next word in the buffer that's the same as this one, get it? It will dynamically yank the word under the cursor into the command as part of the macro. There might be something like CTRL-W in the command above that does this for the whole line, but I don't know it offhand.

杀手六號 2024-08-17 19:48:20

研究了 sykora 和 Matt 的答案后,我提出了一个稍微更好的改进,它是在逐行的基础上而不是逐字的基础上工作的(根据 马特的解决方案)。

光标定位到要删除的行后执行此命令:

:.+1,$s/<CTRL-R>=getline(".")<CR>\n//g

VIM 初学者说明:

:.+1,$s/

从当前行之后的行开始替换(.+1) 和文件结束符 ($)

<CTRL-R>=getline(".")<CR>\n

指的是实际按下 CTRL 和 R 键,这允许您输入特殊表达式。在本例中,=getline(".") 检索当前行的文本。

指按下回车键,完成 表达式。我还在搜索模式末尾指定了换行符 (\n),因为我需要将其删除。

//g

然后什么都不替换。

Having studied sykora's and Matt's answers, I came up with a slightly better improvement which works on a line-wise basis rather than a word-wise basis (as per Matt's solution).

This command is to be executed after the cursor is positioned on the line of which the deletion is to occur:

:.+1,$s/<CTRL-R>=getline(".")<CR>\n//g

Explanation for VIM beginners:

:.+1,$s/

Start substitution from the line after the current line (.+1) and the end-of-file ($)

<CTRL-R>=getline(".")<CR>\n

<CTRL-R> refers to the actual pressing of the CTRL and R keys, which allows you to enter special expressions. In this case, =getline(".") which retrieves the text of the current line.

<CR> refers to the pressing of the enter key, which completes the <CTRL-R> expression. I also specify the newline character (\n) at the end of the search pattern as I need to remove it.

//g

And then replace with nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文