Globally append to line with matching term in vim

2020-05-23 02:40发布

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?

I guess I could do

%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?

标签: vim
2条回答
一夜七次
2楼-- · 2020-05-23 03:18

As mentioned, :g// is what you're after, but one further efficiency for your particular need is to run a normal command as part of the global. s is just one of a bunch of commands that :g// can take. You could also d(elete), j(oin), and pretty much whatever ex commands you can imagine.

Another useful one is norm(al), which can be used to execute any normal command. From the :help on :norm(al): "Commands are executed like they are typed."

So you could also achieve what you want with:

:g/Thing to find/norm Astuff to append

Let's say I'm duplicating my mysql config file for my test environment. I want to append _test to every line that starts with "database=", so:

g/^database=/norm A_test

The thing to remember is that Vim will execute everything after 'norm' as if you had typed it in. So no space between the A command and the text to be appended (or you will get an extra space in the output).

查看更多
欢心
3楼-- · 2020-05-23 03:43

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/
查看更多
登录 后发表回答