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

How do I get exact current and total counts for my last search pattern in Vim?

Answer

:echo searchcount({'recompute': 1})

Explanation

Vim can show match counts for your last search, but in large files or after big edits the cached values may lag. searchcount() gives programmatic access to match statistics, and the recompute flag forces a fresh scan so the numbers are trustworthy before you script decisions around them.

How it works

  • searchcount() returns a dictionary describing the current search state
  • {'recompute': 1} tells Vim to rescan instead of reusing cached data
  • The result includes keys like current, total, exact_match, and incomplete
  • :echo prints the dictionary so you can inspect the values immediately

This is especially useful in mappings, statusline functions, and cleanup workflows where stale counts can lead you to skip or overrun edits.

Example

You searched for: TODO
Then made several substitutions and deletions.
Now you want accurate remaining-match counts before running another pass.

Run:

:echo searchcount({'recompute': 1})

Typical output:

{'current': 3, 'total': 11, 'exact_match': 1, 'incomplete': 0, ...}

Tips

  • Use let sc = searchcount({'recompute': 1}) inside a function for conditional logic
  • If you only need speed, omit recompute and use cached values
  • Pair this with :set hlsearch to visually confirm the numeric result

Next

How do I restrict keyword completion to current buffer, windows, buffers, and tags for predictable results?