<p>In order to know how many times a pattern exists in current buffer, I do:</p>

<pre><code>:%s/pattern-here/pattern-here/g
</code></pre>

<p>It gives the number of occurrences of the pattern, but is obviously cumbersome and also has the side-effect of setting the 'changed' status.</p>

<p>Is there a more elegant way to count?</p>

[{'answer_id': 70657, 'author': 'Bruno De Fraine', 'author_id': 6918, 'author_profile': 'https://Stackoverflow.com/users/6918', 'pm_score': 8, 'selected': True, 'text': '<p>To avoid the substitution, leave the second pattern empty, and add the “n” flag:</p>\n\n<pre><code>:%s/pattern-here//gn\n</code></pre>\n\n<p>This is described as <a href="http://vimdoc.sourceforge.net/htmldoc/tips.html#count-items" rel="noreferrer">an official tip</a>.</p>\n'}, {'answer_id': 70690, 'author': 'Ilya', 'author_id': 6807, 'author_profile': 'https://Stackoverflow.com/users/6807', 'pm_score': 3, 'selected': False, 'text': '<pre><code>:!cat %| grep -c "pattern"\n</code></pre>\n\n<p>It\'s not exactly vim command, but it will give you what you need from vim.<br>\nYou can map it to the command if you need to use it frequently.</p>\n'}, {'answer_id': 1168304, 'author': 'redacted', 'author_id': 53726, 'author_profile': 'https://Stackoverflow.com/users/53726', 'pm_score': 2, 'selected': False, 'text': '<p>The vimscript <a href="http://www.vim.org/scripts/script.php?script_id=1682" rel="nofollow noreferrer">IndexedSearch</a> enhances the Vim search commands to display "At match #N out of M matches".</p>\n'}, {'answer_id': 8073228, 'author': 'rdeits', 'author_id': 641846, 'author_profile': 'https://Stackoverflow.com/users/641846', 'pm_score': -1, 'selected': False, 'text': '<p>vimgrep is your friend here:</p>\n\n<pre><code>vimgrep pattern %\n</code></pre>\n\n<p>Shows:</p>\n\n<pre><code>(1 of 37)\n</code></pre>\n'}, {'answer_id': 14635785, 'author': 'konyak', 'author_id': 1408866, 'author_profile': 'https://Stackoverflow.com/users/1408866', 'pm_score': 3, 'selected': False, 'text': "<pre><code>:help count-items\n</code></pre>\n\n<p>In VIM 6.3, here's how you do it.</p>\n\n<pre><code>:set report=0\n:%s/your_word/&amp;/g    # returns the count without substitution\n</code></pre>\n\n<p>In VIM 7.2, here's how you'd do it:</p>\n\n<pre><code>:%s/your_word/&amp;/gn   # returns the count, n flag avoids substitution\n</code></pre>\n"}, {'answer_id': 32634262, 'author': 'Sagar Jain', 'author_id': 3345302, 'author_profile': 'https://Stackoverflow.com/users/3345302', 'pm_score': 1, 'selected': False, 'text': '<p>Put the cursor on the word you want to count and execute the following.</p>\n\n<pre><code>:%s/&lt;c-r&gt;&lt;c-w&gt;//gn\n</code></pre>\n\n<p>See <code>:h c_ctrl-r_ctrl-w</code></p>\n'}]
['https://Stackoverflow.com/questions/70529', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6984/']