I know that if you press n you can hop to the next occurrence of a matching pattern, and that if you want to go to the previous match, you press N.

But if my cursor is already in the middle of a match, how do I tell what number match that is from the top of the document?

  • The answers to this StackOverflow question tend to show the number of the current match as well as the total number of matches. My solution displays (3/5) in the statusbar, although it counts matching lines, not matches. Aug 19, 2015 at 21:38

3 Answers 3


You can use the n flag with the substitution command.


Or use the current search pattern via :%s///gn.

You can also use :vimgrep and the quickfix list. Search for your pattern via:

:vimgrep/set/ %

This searches the current file, %, and adds the matches to the quickfix list. Then you can move through the quickfix list via :cnext or :cprevious. Upon moving through the quickfix list text will display at the bottom showing something like this (4 of 10). By using the :copen command a window showing the quickfix results will open. Move to the pattern via pressing <cr> on a quickfix item.

However I prefer to use a plugin for this. vim-indexed-search gives you a message in the command line such as:

2 of 32 matches

This will tell what number match that is from the top of the document.

  • 2
    There's a caveat with :vimgrep/set/ %, that you have to already have the file saved, but it works great! Thanks!
    – leeand00
    Aug 19, 2015 at 12:01

You can do this with one command :


Decomposing :

:1,.s/      " start a substitute command from top of the document to current line
<C-r><C-w>  " paste the content of the word under the cursor
//          " replace with noting
gn          " don't actually replace

And in normal mode you can create a mapping for better use :

nnoremap <leader>n :1,.s/<C-r><C-w>//gn<CR>

See :h c_<C-R>_<C-W> for further references.

Edit If you want to rely on your previously entered search pattern, you can replace <C-R><C-W> by <C-R>/. As the / register is the search register, it contains the last search pattern.


This should just be a comment but as I don't have the reputation to comment here goes.

This won't be accurate for multi occurrences on the one line but


To discombobulate

`yiw` will select the yank the word under the cursor  to register `"` .

`:1,.` does something from line 1 to the current line in command mode 

`<C-R>"` means Ctrl-r" which pastes the contents of register " to the command line

`s/<C-R>"//gn` searches for the value in `"` or `yes` and replaces nothing but counts matches. 

eg for the following with the cursor on a yes in line 5 the above will say 10 matches on 5 lines

1    yes no yes
2    yes no yes
3    yes no yes
4    yes no yes
5    yes no yes
6    yes no yes
7    yes no yes
  • 2
    viwy -> yiw (visual detour unnecessary)
    – VanLaser
    Aug 19, 2015 at 2:45
  • 1
    Yeah my bad what I was doing wasn't working so I did it the roundabout way . I'll update
    – Steve
    Aug 19, 2015 at 2:56
  • 1
    You don't even need to yiw. You can instead put <cword> in the substitute command and it will be replaced with the current word under the cursor on execution. :help <cword> for more details and other such keywords.
    – tokoyami
    Aug 19, 2015 at 11:56
  • <cword> was something I learned form this thread and bery useful it was to :)
    – Steve
    Aug 19, 2015 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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