-2

I have a large config file, where many different instances of a particular config format appear. I would like to take every line where a particular word appears and add some text after it.

Specifically I have configs that look like this:

suite: arc-cts-qual                                                                  
pool: MANAGED_POOL_CTS                                                           
exclude_boards: coral, grunt, octopus, rammus, nami, hatch                           
branch_specs: ==tot-2                                                                

I would like to find every config that has MANAGED_POOL_CTS, and add the line

pubsub_topic: "projects/foo/topics/bar"

So that it will make the above config look like

suite: arc-cts-qual                                                                  
pool: MANAGED_POOL_CTS                                                           
pubsub_topic: "projects/foo/topics/bar"
exclude_boards: coral, grunt, octopus, rammus, nami, hatch                           
branch_specs: ==tot-2                                                                

If this was a contiguous block of configs, I could do it easily with :'<,'>s/pool: MANAGED_POOL_CTS/<etc.>/. But it isn't, so I'd like to be able to act on every line that matched the [I query.

0

1 Answer 1

3

Check out :help global. It basically filters out the lines that don't match the pattern. So you can do :'<,'>global/<pattern to select lines based on>/substitute/pool: MANAGED_POOL_CTS/<etc.>/.

Or :g/MANAGED_POOL_CTS/normal! opubsub_topic: "projects/foo/topics/bar". (Edited with @statox's example)

Or, you could execute a macro. Something like: :global/<pattern>/normal @w

8
  • I know that exists. It doesn't answer my question. Aug 8, 2020 at 22:22
  • @JacobKopczynski I don’t understand. You can do as you described with a global + macro. Please, what is not succeeding? Aug 8, 2020 at 23:23
  • 1
    @JacobKopczynski If you need the user to give more details in your answer you should specify why the answer doesn't apply to your situtation. Here based on Ari's solution I think you could write something like :g/MANAGED_POOL_CTS/normal! opubsub_topic: "projects/foo/topics/bar", if that is not the case please explain why it doesn't fit your needs.
    – statox
    Aug 9, 2020 at 15:22
  • 2
    @AriSweedler For improved clarity your answer would benefit of a more complete explanation of how to combine :g and :s and of the actual command which would work on the sample provided by OP. Otherwise it might not be easy for all readers to actually use it.
    – statox
    Aug 9, 2020 at 15:23
  • 1
    @statox thank you for the advice. I appreciate the moderation, there was a disconnect that I did not know how to address that I think you have handled wonderfully. If you don’t mind, I will edit my answer to include your example Aug 9, 2020 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.