4

When I search for something /search-term and it shows up more than 100 times in my file, vim shows me that I'm on match x/>99 instead of x/121 etc.

For smaller numbers it shows me the exact number of total matches: 10/23, but with more than 99 total matches it doesn't show the exact number of matches anymore.

How do I make it show me the total exact number of matches anyway?

  • 1
    not possible, this limit is hard coded for performance reasons. – Christian Brabandt Jul 1 at 9:25
6

I assume you are talking of the built-in search counter here enter image description here

First for users who don't know, this search counter is controlled by the S flag in shortmess :h 'shortmess'

  S     do not show search count message when searching, e.g.
        "[1/5]"

but the doc doesn't mention how to change the limit, so let's have a look at vim source code:

here we have the define #define SEARCH_STAT_DEF_MAX_COUNT 99.

This value is passed to cmdline_search_stat here.

And here in cmdline_search_stat you can see how this value is used. When there is more than 99 matches the source code will use this limit:

if (stat.incomplete == 1)
    vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
else if (stat.cnt > maxcount && stat.cur > maxcount)
    vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]", maxcount, maxcount);
else if (stat.cnt > maxcount)
    vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]", stat.cur, maxcount);
else
    vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", stat.cur, stat.cnt);

So you can't change that without changing Vim's sources.

And for neovim the answer is the same because the code is pretty similar it just doesn't use a constant to hold the 99 limit.

| improve this answer | |
  • Is it possible in neovim? – theonlygusti Jul 1 at 9:47
  • Probably not but I'll let you check the docs and the code by yourself :) – statox Jul 1 at 9:53
  • I don't think so, since this feature comes from a vim patch and was later merged into neovim. – Christian Brabandt Jul 1 at 10:21
  • @ChristianBrabandt Indeed the code is pretty similar – statox Jul 1 at 10:52
6

If your Vim binary includes the patch 8.2.0877, you can get the search statistics via the searchcount() function. And the latter is not limited to 99 matches.

You can invoke it right after a / search with a CmdlineLeave autocmd, and right after a n motion by installing a wrapper mapping around the latter.

Example:

const s:MAXCOUNT = 1000
const s:TIMEOUT = 500

augroup index_after_slash | au!
    au CmdlineLeave /,\? call s:index_after_slash()
augroup END

fu s:index_after_slash() abort
    if getcmdline() is# '' || state() =~# 'm'
        return
    endif
    call timer_start(0, {-> mode() =~# '[nv]' ? s:search_index() : 0})
endfu

fu s:search_index() abort
    try
        let result = searchcount(#{maxcount: s:MAXCOUNT, timeout: s:TIMEOUT})
        let [current, total, incomplete] = [result.current, result.total, result.incomplete]
    catch
        echohl ErrorMsg | echom v:exception | echohl NONE
        return ''
    endtry
    let msg = ''
    let pat = substitute(@/, '\%x00', '^@', 'g')
    if incomplete == 0
        let msg = printf('[%*d/%d] %s', len(total), current, total, pat)
    elseif incomplete == 1 " recomputing took too much time
        let msg = printf('[?/??] '..%s', pat)
    elseif incomplete == 2 " too many matches
        if result.total == (result.maxcount+1) && result.current <= result.maxcount
            let msg = printf('[%*d/>%d] %s', len(total-1), current, total-1, pat)
        else
            let msg = printf('[>%*d/>%d] %s', len(total-1), current-1, total-1, pat)
        endif
    endif
    if strchars(msg, 1) > (v:echospace + (&cmdheight-1)*&columns)
        let n = v:echospace - 3
        let [n1, n2] = n%2 ? [n/2, n/2] : [n/2-1, n/2]
        let msg = matchlist(msg, '\(.\{'..n1..'}\).*\(.\{'..n2..'}\)')[1:2]->join('...')
    endif
    echo msg
    return ''
endfu

nmap n <plug>(n)<plug>(search_index)
nmap N <plug>(N)<plug>(search_index)
nno <plug>(n) n
nno <plug>(N) N
nno <expr> <plug>(search_index) <sid>search_index()

searchcount() won't show a total amount of matches greater than 1000. If that's not enough, increase s:MAXCOUNT. And it will stop trying to compute the number of matches after half-a-second. If that's too long, decrease s:TIMEOUT.

Note that increasing s:MAXCOUNT and s:TIMEOUT may have a negative impact on Vim's performance. The values used in the previous snippet work for me; they may or may not work for you. I guess it depends on the machine you're using and/or on the patterns you're usually looking for. Take that into consideration before setting these parameters.

You can improve the performance by rewriting the code in Vim9 script:

vim9script

const MAXCOUNT = 1000
const TIMEOUT = 500

augroup index_after_slash | au!
    au CmdlineLeave /,\? IndexAfterSlash()
augroup END

def IndexAfterSlash()
    if getcmdline() == '' || state() =~# 'm'
        return
    endif
    timer_start(0, {-> mode() =~# '[nv]' ? SearchIndex() : 0})
enddef

def SearchIndex(): string
    let incomplete: number
    let total: number
    let current: number
    let result: dict<any>
    try
        result = searchcount({'maxcount': MAXCOUNT, 'timeout': TIMEOUT})
        current = result.current
        total = result.total
        incomplete = result.incomplete
    catch
        echohl ErrorMsg | echom v:exception | echohl NONE
        return ''
    endtry
    let msg = ''
    let pat = getreg('/')->substitute('\%x00', '^@', 'g')
    if incomplete == 0
        msg = printf('[%*d/%d] %s', len(total), current, total, pat)
    elseif incomplete == 1 # recomputing took too much time
        msg = printf('[?/??] %s', pat)
    elseif incomplete == 2 # too many matches
        if result.total == (result.maxcount + 1) && result.current <= result.maxcount
            msg = printf('[%*d/>%d] %s', len(total - 1), current, total - 1, pat)
        else
            msg = printf('[>%*d/>%d] %s', len(total - 1), current - 1, total - 1, pat)
        endif
    endif
    if strchars(msg, 1) > (v:echospace + (&cmdheight - 1) * &columns)
        let n = v:echospace - 3
        let n1 = n % 2 ? n / 2 : n / 2 - 1
        let n2 = n / 2
        let matchlist = matchlist(msg, '\(.\{' .. n1 .. '}\).*\(.\{' .. n2 .. '}\)')
        msg = matchlist[1] .. '...' .. matchlist[2]
    endif
    echo msg
    return ''
enddef

nmap n <plug>(n)<plug>(search_index)
nmap N <plug>(N)<plug>(search_index)
nno <plug>(n) n
nno <plug>(N) N
nno <expr> <plug>(search_index) <sid>SearchIndex()

For more info, see:

| improve this answer | |
  • 2
    Really cool answer! Maybe it would be worth adding a warning about the potential performances issues when you increase s:MAXCOUNT and s:TIMEOUT because originally that's why vim has the limits I mention in my answer. But anyway I didn't know this function so that a very interesting answer. – statox Jul 2 at 7:09
2

I use Anzu which counts up to 1000 results by default.

You can change this limit:

let g:anzu_search_limit = 1000
| improve this answer | |
2

There are several plugins that support showing the number of matchings, for example, vim-indexed-search. In the alternatives section, you can also find several similar plugins.

| 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.