09 December 2025

How can I count the number of a specific character, e.g. '|', in a line, in vim?

Show the count visually using :s

This replaces nothing, but prints the count per line:

:s/|//gn

Explanation:

  • s = substitute

  • | = the character you are counting

  • g = find all occurrences, not just the first

  • n = report the count instead of replacing

Vim will show something like:

3 matches on 1 line

Run it on multiple lines by selecting a range (e.g., entire file):

:%s/|//gn

No comments:

Post a Comment