131

Is it possible to count how many times a word or a pattern appears in a file? This is sometimes useful to find out how many times a function has been called, etc.

2

7 Answers 7

186

Quincy's answer is fine, but there's an exact way to do this which doesn't require editing the buffer:

:%s/pattern//ng

This will print a message like 3 matches on 2 lines, and no changes will be made to your buffer.

The n flag makes the :substitute command print the number of matches instead of performing an actual substitution; the g flag enables reporting of multiple matches per line.


Another thing that might be useful to your use case is to print all lines that match a pattern:

:global/pattern/print

which can be shortened to:

:g/pattern

This is one of the simplest uses of the :global command (which is mind-bogglingly powerful). It will simply print out all of the lines that match pattern, and then (if there is more than one line) you press Enter or type another command to make it go away.

A bit of trivia: This command is the origin of the name grep, as it would commonly be described as g/re/p, where re stands for "regular expression".

3
  • 1
    I have to do this maybe 2-3 times a year and never remember the syntax so it's to the search machine, each time. Just wanted to say thank you for this clear answer because it's been there for me more than once and has, for a couple of years now, saved me that gawd awful wikia page! (wikia, not the page's content)
    – Will
    Commented Aug 13, 2018 at 20:06
  • 3
    Minor bit: the command g/re/p is actually from ed, not vi. Grep predates vi by a couple of years.
    – Will
    Commented Aug 13, 2018 at 20:30
  • 2
    I have a nnoremap <leader>n :%s///gn<CR> mapping. So I press <leader>n to count the occurrences of the previously searched pattern. I use this surprisingly often.
    – Rolf
    Commented Feb 6, 2019 at 7:51
38

Nowdays (meaning Vim 8.1.1270) you can use:

set shortmess-=S

to enable a count of [x/y] showing in the bottom right corner every time you do a / or ? search.

native search count

Relavant section from the Vim help

Note that if the search finds more than 99 results, Vim unfortunately just shows [x/>99]:

native search count over 99

For this reason, I personally use google/vim-searchindex, which works for any amount of results:

vim-search-index search count

(By default the plugin is limited to files with "only" less than 100k lines, this can be adjusted with let g:searchindex_line_limit=1000000 though.)

6
  • 1
    This does not work for my Vim of version 7.4, am I doing something wrong?
    – zyy
    Commented Jan 6, 2021 at 19:52
  • 2
    @zyy It got introduced in 8.1.1270. I suggest that you either update your Vim, or use the plugin I linked.
    – ruohola
    Commented Jan 6, 2021 at 20:16
  • 1
    Another edge-case of importance: if you're on x > 99, the result is just [>99/>99], which is completely and utterly useless Commented Apr 21, 2021 at 22:07
  • 1
    Yes @Zoe, that's why the plugin I linked is awesome.
    – ruohola
    Commented Apr 21, 2021 at 22:08
  • 1
    Just tested that, but it's incompatible with a search preview plugin I use. Fun :) Commented Apr 21, 2021 at 22:16
19

:%s/pattern//n The n flag in the end tells :s command to report the number of matches and not actually substitute. Read :h :s_flags for more details.

2
  • 6
    Isn't it gn?
    – yo'
    Commented Feb 3, 2015 at 18:50
  • 3
    @yo' Yes, though g is just another flag, you'd want to use g too if you feel there may be more than one matches per line, which is often true. For the purpose of the question however the flag n is more important. Commented Feb 3, 2015 at 18:53
11

First use / to search for a regex, then

:%s///gn

This is the same as Dhurva's answer, except that it a.) Is easier to use (since you can preview the results of what your regex matches first) and b.) Matches globally with the g flag to count all occurrences.

10

Thanks to this PR https://github.com/vim/vim/pull/4317 , each search command shows search statistics (like current match position, and number of matches) in vim 8.1.1270. Make sure to remove S from the variable shortmess.

3

:%s/pattern//g

You will see the number of substitutions in the status bar. That is how many times the pattern appears. Then just press u to undo the substitutions.

1
  • 2
    This is such a horrible solution, but I love it.
    – Sahil
    Commented Aug 12, 2020 at 9:17
3

A mapping I added to my .vimrc based on the answers here:

" count nr of occurrences of word under cursor
nnoremap <leader>c :%s/<c-r><c-w>//gn<cr>

" count nr of occurrences of visual selection
vnoremap <leader>c :<c-u>%s/<c-r>*//gn<cr>

A bit of explanation, hopefully helpful for newer vimmers:

  • <c-r><c-w> inserts the word under the cursor in the command line, handy in many occasions.

  • The <c-u> is needed to remove the automatically inserted '<,'> when pressing : in visual mode and going to the command line. The * register contains the (last) visual selection, <c-r>* inserts the contents of the * register in the command line (can also be used in insert mode).

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.