DEV Community

Colin Bartlett
Colin Bartlett

Posted on • Originally published at vimtricks.substack.com

2

VimTrick: Non-greedy pattern matching

Good morning Vim users of Dev.to! Another Monday has arrived and that means another in my series of Vim tricks and tips...

You might already know that pattern matching in Vim is greedy by default. That means, it will select the largest string that matches your pattern. For example, suppose you have:

{
  "name": "Andy",
  "last": "Dufresnes",
  "id": 37927
}
Enter fullscreen mode Exit fullscreen mode

If you pattern match with /”.*”, Vim will select the entire string on that line: "name": "Andy". But what if what we really want is just the first "name"?

Instead of .* use .\{-} and Vim will select just the smallest string that matches the pattern instead of the largest.

I have screenshots and more details over on my post on VimTricks.

Let me know if you find this helpful! I still have not internalized it for every day use but I've found it very helpful when I do recall it!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay