Skip to main content
  1. Posts/

How to Match Non-greedily in Nvim/Vim

·120 words·1 min·
Nvim Regex

A short note on how to match non-greedily in Nvim/Vim.

Vim’s regex is slightly different from the usal regex we see. Vim use - to indicate non-greedy match, for example, to match non-greedily for a.*c, we can use the following search:

\va.{-}c

Note that \v is used to simplify the code. Otherwise, the command will become ugly and error-prone.

Here is a list of non-greedy syntax (the following assumes that \v is added):

  • {-n,m}: match n to m previous pattern, non-greedy
  • {-n,}: match at least n of previous pattern, non-greedy
  • {-,m}: match 0 to m of previous pattern, non-greedy
  • {-}: match 0 or more of previous pattern, non-greedy (non-greedy version of *)

For more info, read :h non-greedy inside Vim.

Related

Nifty Nvim/Vim Techniques That Make My Life Easier -- Series 11
··376 words·2 mins
Nvim Regex
An Introduction to Lookaround Regular Expression in Neovim/Vim
··614 words·3 mins
Nvim Vim Regex
Notes on Using GCP Logging
·345 words·2 mins
Note GCP Logging Regex