created 2004 · complexity basic · author Hans fugal · version 6.0
It's easy to count the total number of words, or the number of occurrences of a particular word, and more.
Counting number of wordsEdit
To count the number of words in the current buffer, press
g then Ctrl-g.
To count the number of words in a block, select the block (for example, type
V5j to select 6 lines) and again press
g then Ctrl-g.
The output looks like this:
Selected 6 of 358 Lines; 37 of 2281 Words; 186 of 13426 Bytes
If you prefer, you can call an external program using the "%" file name expansion. For example, in Unix you could do the following to get the word count of the entire file:
:w :!wc %
:w writes the current file so that the external program can read it. An alternative (which does not require saving the buffer to a file), is to use the command:
The above writes the current buffer to the standard input of the external
wc program. :help :write_c
Counting number of printed words in a LaTeX documentEdit
If you LaTeX and you need a printed word count (for a journal paper, etc.), wc's output isn't directly useful, since its count includes all markup and other non-printed parts of the file. If you LaTeX in Windows, use something like detex to strip the markup away, leaving the actual text, before piping to wc. Unxutils or Cygwin will be of interest to Windows users needing utilities taken for granted by *nix users, including wc.
An example mapping to F3 is given below. The current buffer is written to the stdin of detex, which writes the detex-ed file to the stdin of wc. If something is selected in visual, visual-block, or visual-line mode, only the selection is counted, otherwise the entire buffer is counted.
:map <F3> :w !detex \| wc -w<CR>
In Windows, this could be made less obtrusive by returning wc's output to a discreet error message at the bottom of the buffer or window, rather than popping up a console every time. Suggestions are welcome. I was surprised to not find anything like this in existing Vim scripts, or packaged with vim-latex, which I've used for quite a while. Please replace this section with the relevant link if such a thing exists.
EDIT: Texcount Perl script can be also used for this matter, which is quite a clever piece of software, but unfortunately, it does not count everything perfectly.
Counting occurrences of a patternEdit
:s substitute command with the
n flag (counts number of hits; no changes occur) to count the number of occurrences of a pattern. For example:
Combine this with the ability to Search and replace in a visual selection, and you can count the occurrences in a portion of a file as well!
For example to count space delimited words in a file you could use:
A quick way to list all occurrences of the word under the cursor it to type
[I (which displays each line containing the current keyword, in this file and in included files when using a language such as C). :help [I
The WC plugin counts words according to "Strunk & White" rules, for fiction, novel, or journalism writing.
The SearchPosition plugin shows the relation to search pattern matches in a range or buffer. The mappings, command and operator provided by this plugin search a range or the entire buffer for a pattern (defaulting to the current search pattern), and print a summary of the number of occurrences above, below and on the current line, for example:
1 match after cursor in this line, 8 following, 2 in previous lines; total 10 for /\<SearchPosition\>/ 5 matches in this fold, 9 before, 6 following; total 21 for /endif/ On sole match in this line, 40 in following lines for /let/ :144,172 7 matches in this fold for /let/