I've seen this question:

Is there a way to count the number of occurrences of a word in a file?

but most of the time I search using * or just /.

It is possible to display automatically in the statusline the number of matches when using these commands?

  • I think you should be able to use the function I posted here replace a:word by getreg("/") and then use the function in your status line. You will probably need to try...catch to handle unset search register and not found occurrences. – statox Apr 16 '18 at 13:46
  • at the very least, you want to enable caching or else this will most likely slow down your vim considerably, since the statusline is evaluated often, really often. – Christian Brabandt Apr 16 '18 at 14:58
  • 1
    @ChristianBrabandt maybe a solution to avoid caching would be to use a variable in the statusline set by the function and use autocommands to execute the function? – statox Apr 16 '18 at 15:15
  • 2
    @statox as long as you do not change the variable everytime the function is run I would still call this caching. But yes this would be a solution. – Christian Brabandt Apr 16 '18 at 15:27

If you use the vim-airline Plugin together with the vim-anzu Plugin, the search statistics are already displayed in the statusline. See the below screenshot (and note the [1/3])

Airline with vim-anzu


vim 8.1.1270 (May 2019) :set shortmessage-=S

Since May 2019 vim offers the possibility to show the count in the statusline (as long as it less than 99) which however is disabled by default. Add to your vimrc

set shortmessage-=S

or activate it on demand with

:se shm-=S

Following additional information can be found under :h search-commands

When 'shortmess' does not include the "S" flag, Vim will automatically show an index, on which the cursor is. This can look like this:

[1/5]       Cursor is on first of 5 matches.
[1/>99]    Cursor is on first of more than 99 matches.
[>99/>99] Cursor is after 99 match of more than 99 matches.
[?/??]      Unknown how many matches exists,
                   generating the statistics was aborted because of search timeout.

Note: the count does not take offset into account.


Apart from the native support as indicated by @Hotschke, you may want to try vim-indexed-search. This plugin also summarizes several similar plugins in its GitHub homepage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.