vimtricks.wiki Concise Vim tricks, one at a time.

How do I count the words, lines, and characters in just my visual selection?

Answer

g<C-g> (visual mode)

Explanation

In visual mode, pressing g<C-g> reports detailed statistics for the selected text only — word count, character count, and byte count. This is far more useful than the normal-mode version (which counts the whole file) when you need to measure a specific excerpt, passage, or code block.

How it works

  • Enter visual mode (v, V, or <C-v>) and select the text
  • Press g<C-g> to display the selection statistics in the statusbar
  • Output format: Selected N of M Lines; N of M Words; N of M Bytes

Example

Select two lines of text with Vjj, then press g<C-g>:

Selected 2 of 312 Lines; 18 of 2341 Words; 97 of 14032 Bytes

This shows 18 words and 97 bytes in your 2-line selection, out of the full file's totals.

Tips

  • Works with all three visual modes: character (v), line (V), and block (<C-v>)
  • In normal mode, g<C-g> shows the same stats for the entire file
  • The word count algorithm matches Vim's internal word boundaries (split on whitespace)
  • Useful when checking blog post excerpts, commit message lengths, or doc sections against word limits

Next

What is the difference between the inner word (iw) and inner WORD (iW) text objects in Vim?