全局附加到 vim 中匹配术语的行 [英] Globally append to line with matching term in vim

查看:11
本文介绍了全局附加到 vim 中匹配术语的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这很容易,我只是缺少一两个字符.

I am sure this is easy, I am just missing a character or two.

我需要在文件中搜索特定术语,当我找到它时,我需要在该行中附加一些内容.我想对比赛的每一行都这样做.

I need to search for a particular term in a file, and when I find it, I need to append something to that line. And I want to do that for EVERY line with the match.

要做一次,我可以这样做:

To do it once, I can do this:

/Thing to find/s/$/ Stuff to append/

简单.如果我的要找的东西"在行尾,我可以这样做:

Easy. And if my "thing to find" were at the END of the line I could do this:

%s/\(Thing to find\)$/\1 Stuff to append/

在每个匹配的行上做同样的事情

To do the same thing on every matching line

但是我如何在每一行做第一件事?

But how do I do the first thing on every line?

我想我可以做到

%s/\(Thing to find.*\)$/\1 Stuff to append/

但这感觉很笨拙,而且如果要查找的东西不止一次出现在一条线上,就会使事情变得更加复杂.

But that feels clumsy and would make it more complicated if the thing to find were on a line more than once.

我想一定有一种方法可以让我在任何地方进行第一次搜索,但我无法编写足够简洁的描述来谷歌搜索.

I am thinking there must be a way to just do my first search everywhere, but I am having trouble writing a concise enough description to google it.

如此强大的堆栈器,有人想用两字节的解决方案来打我吗?

So mighty Stackers, anyone want to nerd slap me with a two byte solution?

推荐答案

:g// 命令就是你要找的——它在匹配模式的每一行上运行一个命令:

The :g// command is what you're looking for — it runs a command on each line matching a pattern:

:g/Thing to find/ s/$/ Stuff to append/

这篇关于全局附加到 vim 中匹配术语的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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