Search code examples
vimreplace

How to count search results in Vim?


How to find in the text file using Vim how many times specific word repeats.

For example, I want to see how many times word "name" repeats in the code:

"collection": "animals",
    "fields": [
      {
        "field": "id",
        "name": ""
      },
      {
        "field": "age",
        "name": ""
      },
      {
        "field": "origin",
        "name": ""
      }

Result - 3


Solution

  • You can count the number of matches using the n flag in the substitute command. Use the following to show number of times that some-word matches text in current buffer:

    :%s/some-word//gn
    

    You can read all the details on the vim tips wiki