Vim Tips Wiki
No edit summary
 
(23 intermediate revisions by 15 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=689
 
|id=689
  +
|previous=688
|title=Word Count
 
  +
|next=690
|created=March 29, 2004 22:26
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=Hans fugal
 
|author=Hans fugal
|version=5.7
+
|version=6.0
 
|rating=433/175
 
|rating=433/175
  +
|category1=
|text=
 
  +
|category2=
To count the words in a file: g<ctrl-g>
 
 
To count the words in a block, select the block and once again g<ctrl-g>
 
 
 
 
The output looks something like this:
 
 
Selected 6 of 358 Lines; 37 of 2281 Words; 186 of 13426 Bytes
 
 
 
 
For more information, :help 12.5 (http://vimdoc.sourceforge.net/htmldoc/usr_12.html#12.5)
 
 
}}
 
}}
  +
It's easy to count the total number of words, or the number of occurrences of a particular word, and more.
   
  +
==Counting number of words==
== Comments ==
 
  +
To count the number of words in the current buffer, press <code>g</code> then Ctrl-g.
for the entire file, in Unix, you could also use the magical "%" file name expansion, e.g
 
:w
 
:!wc %
 
 
dh
 
 
 
dotan.haREMOVETHISlevi--AT--intel.com
 
, March 30, 2004 9:45
 
----
 
g&lt;c-g&gt; is useful, thanks. - Mosh
 
 
mosh
 
, April 7, 2004 19:35
 
----
 
A related topic is how to count particular words, like how many times does the word "word" appear in this text. The old trick for this is to:
 
:%s/word/&amp;/g
 
Then Vim says something like:
 
4 substitutions on 4 lines
 
and you have your answer.
 
-- Chris X Edwards
 
 
'''Anonymous'''
 
, June 19, 2004 6:56
 
----
 
Very useful, thank you.
 
 
'''Anonymous'''
 
, August 11, 2004 1:20
 
----
 
great tip with word count :)
 
 
'''Anonymous'''
 
, February 25, 2005 12:51
 
----
 
Hallo,
 
 
how is it possible to store the number of a word count into a variable?
 
 
Example:
 
   
  +
To count the number of words in a block, select the block (for example, type <code>V5j</code> to select 6 lines) and again press <code>g</code> then Ctrl-g.
"This is a sentence"
 
   
 
The output looks like this:
These are 4 words.
 
  +
<pre>
 
Selected 6 of 358 Lines; 37 of 2281 Words; 186 of 13426 Bytes
  +
</pre>
   
  +
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:
My goal is to have a variable like
 
  +
<pre>
 
:w
 
:!wc %
  +
</pre>
   
  +
The initial <code>:w</code> 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:
let count = ...
 
  +
<pre>
  +
:w !wc
  +
</pre>
   
  +
The above writes the current buffer to the standard input of the external <code>wc</code> program. {{help|:write_c}}
count = 4
 
   
  +
==Counting number of ''printed'' words in a LaTeX document==
This allows me to use the word count in a vim script.
 
  +
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 [http://urchin.earth.li/~tomford/detex/index.html detex] to strip the markup away, leaving the actual text, before piping to wc. [http://sourceforge.net/projects/unxutils/ Unxutils] or [http://www.cygwin.com/ 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.
How do I achive this?
 
  +
<pre>
  +
:map <F3> :w !detex \| wc -w<CR>
  +
</pre>
   
  +
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.
Best regards,
 
   
  +
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.
Klaus
 
   
  +
==Counting occurrences of a pattern==
  +
Use the <code>:s</code> substitute command with the <code>n</code> flag (counts number of hits; no changes occur) to count the number of occurrences of a pattern. For example:
  +
<pre>
  +
:%s/pattern//gn
  +
</pre>
   
  +
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:
  +
<pre>
  +
:%s/[^ ]\+//gn
  +
</pre>
   
  +
A quick way to list all occurrences of the word under the cursor it to type <code>[I</code> (which displays each line containing the current keyword, in this file and in included files when using a language such as C). {{help|[I}}
Klaus Horsten
 
, October 16, 2005 3:03
 
----
 
I found a solution now.
 
   
  +
==Related plugins==
Author is Antony in cooperation with Michael E Clark (Posting in a news group).
 
  +
The {{script|id=1191|text=WC plugin}} counts words according to "Strunk & White" rules, for fiction, novel, or journalism writing.
   
  +
The {{script|id=2634|text=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:
let count_words = Num_patterns_in_string(getline("."), '\&lt;\w\+\&gt;')
 
  +
<pre>
echo count_words
 
  +
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/
  +
</pre>
   
  +
==References==
fun! Num_patterns_in_string(str, pat)
 
  +
*{{help|12.5}}
let i = 0
 
let num = -1
 
while i != -1
 
let num = num + 1
 
let i = matchend(a:str, a:pat, i)
 
endwhile
 
return num
 
endfun
 
   
 
==Comments==
Klaus Horsten
 
, October 16, 2005 7:37
 
----
 
<!-- parsed by vimtips.py in 0.457580 seconds-->
 

Revision as of 00:05, 7 February 2013

Tip 689 Printable Monobook Previous Next

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 words

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 %

The initial :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:

:w !wc

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 document

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 pattern

Use the :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:

:%s/pattern//gn

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:

:%s/[^ ]\+//gn

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

Related plugins

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/

References

Comments