Globally append to line with matching term in vim

后端 未结 3 859
萌比男神i
萌比男神i 2021-02-04 04:50

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

3条回答
  •  礼貌的吻别
    2021-02-04 05:07

    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).

提交回复
热议问题