How to Modify Multiple Various Matches at Once in Vim
Pavol Z. Kutaj

Pavol Z. Kutaj @pkutaj

About: A public interface of my current learnings. The focus is on everything from vim, python to cloud. Imperfect. Impersonal. Never too far from my terminal.

Location:
Brno, Czech Republic
Joined:
Jan 26, 2021

How to Modify Multiple Various Matches at Once in Vim

Publish Date: Dec 17 '24
1 0

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

Comments 0 total

    Add comment