[Tutorial] Count the number of occurrences of a pattern in a file in Vim

Published: by Creative Commons Licence (Last updated: )

Count the number of occurrences of a pattern in a file in Vim

:%s/pattern//gn

Explanation:

  • % is the range, which means the whole file.
  • s is the substitute command.
  • pattern is the pattern to search.
  • // is the replacement, which is empty in this case.
  • g is the flag to replace all occurrences in a line.
  • n is the flag to count the number of occurrences.