20

I know I can use wc for counting characters, words and lines of files at the command line.

Is there any way I can can count the number of words while in vim?

21

You can count words and lines inside vi using vi's own counter:

Press g and then CTRL-g. Then the bottom line look for example like this:

Col 1 of 11; Line 1 of 106; Word 1 of 344; Byte 1 of 2644

Or use vi's method to call shell commands:

:w !wc -w

This calls the save (:w) command first and then wc -w and shows the output. Example:

:w !wc -w
344

Press ENTER or type command to continue

Press Enter to go back to vi.

  • It is actually incorrect description of :w !<cmd> construct. It writes the current buffer to a pipe connected to the command. No separate write of the current buffer to a file is promised. Nevertheless, it does what question asked for. – mcepl Nov 17 '17 at 9:45
1

You can also try for :!wc % in Vim, though it counts the size of the file on-disk, not what is in Vim's buffer. This may or may not be what you wanted.

  • This counts what’s on disk (if anything), not what’s in the buffer — so if you have changed the contents of the buffer since the last save, or not saved at all, you’ll get the wrong count (or even an error if you’re creating a file). – Stephen Kitt Mar 20 at 8:57
  • Contra Stephen, this is a correct solution to "How can I count the number of words in a file whilst editing the file in vim", while the accepted answer strictly-speaking isn't (although it's ambiguous what was meant or whether it matters). – Michael Homer Mar 20 at 9:02
  • Funny that it's similar (minus the -w flag to restrict the output to words only) to a delete answer from 2014 by D_Bye (who apparently self-deleted it). – Jeff Schaller Mar 20 at 10:20
0

Plugins such as vim-airline can provide word counts for a file (and selections) as part of a status bar.

  • This answer isn't link-only. I didn't include the specifics of any particular plugin, because (as you say) particular plugins might become outdated. – Patrick Sanan Jan 9 at 19:04
  • I stand corrected. However I would say in the future you should try to include relevant steps to implement the solution you are presenting. Thank you! – kemotep Jan 9 at 19:43
0

For those who want to count number of words in a given piece of text (not whole file), use \S\+ regexp.

  1. Select the text of interest (visual mode)
  2. :s/\S\+//gn

Result. Vim will show you something like this: 10 matches on 1 line

When you hit : in visual mode, vim prepends your command with '<,'> which means to apply the command against the selected text.

\S\+ captures words, that is groups of characters separated by whitespace.

protected by Archemar Mar 20 at 8:44

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.