13

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?

4
  • 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
    Commented Apr 16, 2018 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. Commented Apr 16, 2018 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
    Commented Apr 16, 2018 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. Commented Apr 16, 2018 at 15:27

3 Answers 3

15

vim 8.1.1270 (May 2019) :set shortmess-=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 shortmess-=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.

5
  • @TristanHessell I've rejected your edit because it suggests edits to code; if you wish to improve code in a post please (a) comment on the post and/or (b) post your own answer with improved code. See also When to edit
    – D. Ben Knoble
    Commented May 27, 2020 at 15:51
  • P.S. @Hotschke the option is 'shortmess', but you used shortmessage in a couple of spots; this is what the edit wanted to correct.
    – D. Ben Knoble
    Commented May 27, 2020 at 15:51
  • @D.Ben Knoble: I would have accepted the edit. IMHO it is/was a typo on my side.
    – Hotschke
    Commented May 27, 2020 at 20:19
  • @Hotschke - what do you mean by offset?
    – Shuzheng
    Commented Sep 4, 2020 at 6:19
  • @Shuzheng: I think the copied snippet from vim help refers to /{pattern}/{offset}<CR> (:h /).
    – Hotschke
    Commented Sep 4, 2020 at 11:13
6

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

3

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 and acknowledge you have read our privacy policy.

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