Skip to main content
  1. Posts/

How to Match Non-greedily in Nvim/Vim

·120 words·1 min·

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
An Introduction to Lookaround Regular Expression in Neovim/Vim
··614 words·3 mins
Migrating from Packer.nvim to Lazy.nvim
··651 words·4 mins