I search some words so that there are many highlight results in the current window.
n to move cursor to a matching result, how to know its index of match results easily？
I have tried SearchPosition, but it does not work in my Vim.
I don't know if it's exactly what you want, but to get the number of matches and the index of the current match, I'm using a modified version of this:
nno n n@=Search_and_index()<CR> nno N N@=Search_and_index()<CR> fu! Search_and_index() abort let winview = winsaveview() let [line, col] = [winview.lnum, winview.col] call cursor(1, 1) let [idx, total] = [1, 0] let [matchline, matchcol] = searchpos(@/, 'cW') while matchline && total <= 999 let total += 1 if matchline < line || (matchline == line && matchcol <= col) let idx += 1 endif let [matchline, matchcol] = searchpos(@/, 'W') endwhile call winrestview(winview) echo @/ . '(' . idx . '/' . total . ')' return '' endfu
N to call the function
Search_and_index() which should echo a message such as
The while loop is responsible for incrementing the variables
idx. And at the end of the loop, their values should be respectively the total number of matches of your last search pattern, and the index of the current match (the one under the cursor).
But the loop stops at
999 to avoid taking too much time. You could increase this number if you feel it's too low.