How to Modify Multiple Various Matches at Once in Vim

Pavol Kutaj
1 min readAug 26, 2024

--

  • You need very magic mode to forget about escaping regex syntax!
  • As I am in a data wrangling mode, it’s just wonderful.
  • Start search with \/v<regex>
  • I need to hit multiple matches.
  • Often workspaces fail deployment and I need another retry.
  • Say foo, bar and acme failed. Let’s demo the whole thing
searching for “foo” OR “bar” or “acme” and using g##d, v##d or %s###g
  • These can be substring of comlpicated names. They are unique enough.
  • You can use the | operator to match either "sqs" or "pbs" strings using regex. Here's how you can do it:
\/vfoo|bar|acme
  • Wonderful, we have the match. Now we can operate.
  • As Drew Neil instructed us in Practical Vim, it’s great to decouple search (/ command) and Ex command operating on the search.
  • This regex pattern will match either “sqs” or “pbs” strings in Vim.
  • Then you can use one of my three favorites:
  • :g##d — delete lines with matches
  • :v##d — delete lines without matches
  • :%s##<replace_string>#g — replace matches with a single <replace_string>

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com