VIM 替换:使用当前行作为搜索字符串 [英] VIM substitution: Using the current line as the search string

查看:35
本文介绍了VIM 替换:使用当前行作为搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

PBCPDF05/01/20060.00000.0000PBCPDF0.000006/01/20060.00000.0000PBCPDF0.0082[... 行重复...]

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

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

有没有办法做到这一点?

解决方案

如果您在宏中执行此操作(使用 q 命令),那么当您执行一些更深奥的击键命令时就足够聪明了 - 一个我特别考虑的是CTRL-R,CTRL-W,它们会将光标下的单词插入到命令行中.所以,例如,如果你做了

<前>ESC键qw/[CTRL-R, CTRL-W](你应该看到光标下的单词进入命令行)[进入]q

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

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?

解决方案

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.

这篇关于VIM 替换:使用当前行作为搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆