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

  • If you want to learn how to do this and much much more, you should read through the user guide :) :help usr_01.txt – Ari Sweedler Aug 8 at 0:36
  • Please don't treat me like an imbecile. I'm aware there's a user guide, I'm not new to vim. – Jacob Kopczynski Aug 8 at 22:21
  • 1
    @JacobKopczynski Let's not assume bad intent from people providing help on the internet. Referring you to the manual is not challenging your knowledge or your skills, especially when the user also posted an answer to your question. – statox Aug 9 at 15:19
  • In my experience it is virtually always correct to assume bad intent from people on StackOverflow. People seem to answer questions here not primarily to provide help to others but to inflate their own egos. – Jacob Kopczynski Aug 9 at 18:24
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

| improve this answer | |
  • I know that exists. It doesn't answer my question. – Jacob Kopczynski Aug 8 at 22:22
  • @JacobKopczynski I don’t understand. You can do as you described with a global + macro. Please, what is not succeeding? – Ari Sweedler Aug 8 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 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 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 – Ari Sweedler Aug 9 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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