DEV Community

Pavol Z. Kutaj
Pavol Z. Kutaj

Posted on

How to Modify Multiple Various Matches at Once in Vim

As Drew Neil instructed us in Practical Vim, it's great to decouple search (/) and Ex command (:) operating on the search.
Note: You need very magic mode to forget about escaping regex syntax!
As I am in a data wrangling mode, it's just wonderful.

Step-1: Search with a search command using very magic mode

Start search ith \/v<regex>
I need hit multiple matches.
Often terraform workspaces that failed apply during mass-rollout. I need another retry with the failed ones.
Say foo, bar and acme failed.
These can be substring of complicated names. They are unique enough.

\/vfoo|bar|acme
Enter fullscreen mode Exit fullscreen mode

Wonderful, we have the match. Now we can operate.

Step-2: Modify with an ex command

This regex pattern will match either "foo" or "bar" or "acme" strings

  • Then you can use — for example — one of my three favorites, g##d, v##d, s###g
  • :g##d — delete lines with matches
  • :v##d — delete lines without matches
  • :%s##<replace_string>#g — replace matches with

LINKS

Top comments (0)