Counting characters in vim

Posted by: Brad Waite

Tagged in: Untagged 

There's several ways to count the number of characters in a file within vim.

The 'n' modifier to the substitute operator ('s') tells vim only to report the number of matches, but not to actually do any substitutions.  Given that, the following command will report the number of characters in our file:

:%s/.//gn

That says, "find ('s///n') every occurrence ('g') of any character ('.') in all lines ('%')."  Note that this will not include any line break characters.  To count line breaks, just prepend the '_' modifier to the '.', like so:

:%s/_.//gn

You can also count the characters in a specified range by using a range instead of the '%'.  To count all characters, including line breaks, from lines 20 to 80 do this:

:20,80s/_.//gn

Happy vimming!

Comments (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy