vimtricks.wiki Concise Vim tricks, one at a time.

How do I search for a pattern repeated an exact number of times in Vim?

Answer

/pattern\{3}

Explanation

Vim supports counted quantifiers that let you specify exactly how many times a pattern should repeat. This is far more precise than * or \+ and is essential for matching fixed-width fields, specific indentation levels, or structured data.

How it works

  • \{n} — matches exactly n times
  • \{n,m} — matches n to m times (greedy)
  • \{n,} — matches n or more times
  • \{,m} — matches 0 to m times
  • \{-n,m} — matches n to m times (non-greedy)

Example

Find exactly 3 consecutive digits:

/\d\{3}
Text: 12 345 6789 42
Matches: 345, 678 (first 3 digits of 6789)

Find lines with 4+ spaces of indentation:

/^ \{4,}

Tips

  • \{-} is the non-greedy equivalent of * (match as few as possible)
  • In very magic mode: /\v\d{3} (no backslash needed on braces)
  • Combine with groups: \(ab\)\{3} matches ababab
  • Use \{0,1} as an alternative to \= (optional match)

Next

How do I return to normal mode from absolutely any mode in Vim?