7

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
6

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.

| improve this answer | |
  • @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 May 27 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 May 27 at 15:51
  • @D.Ben Knoble: I would have accepted the edit. IMHO it is/was a typo on my side. – Hotschke May 27 at 20:19
5

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

| improve this answer | |
1

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.

| improve this answer | |

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.