Vim Tips Wiki
(Remove html character entities)
(16 intermediate revisions by 14 users not shown)
Line 3: Line 3:
 
|previous=688
 
|previous=688
 
|next=690
 
|next=690
|created=March 29, 2004
+
|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=
 
|category1=
 
|category2=
 
|category2=
 
}}
 
}}
To count the total words in a file, position the cursor anywhere and type g<ctrl-g> in normal mode.
+
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 total words in a block, select the block and once again type g<ctrl-g>
 
  +
To count the number of words in the current buffer, press <code>g</code> then Ctrl-g.
   
  +
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.
The output looks something like this:
 
   
 
The output looks like this:
Selected 6 of 358 Lines; 37 of 2281 Words; 186 of 13426 Bytes
 
  +
<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:
A related topic is how to count particular words, like how many times does the word "word" appears in this text. You can use ''substitute'' with the <tt>n</tt> flag (counts occurrences; does not change) as follows:
 
  +
<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:
 
<pre>
 
<pre>
 
:w !wc
:%s/word//gn
 
 
</pre>
 
</pre>
   
  +
The above writes the current buffer to the standard input of the external <code>wc</code> program. {{help|:write_c}}
Combine this with the ability to [[Search and replace in a visual selection]], and you can count the occurrences of a word in portions of a file as well!
 
   
  +
==Counting number of ''printed'' words in a LaTeX document==
If you prefer the output of some external program, you can always call that as well, using the "%" file name expansion. For example, in Unix you could do the following to get the word count of the entire file:
 
  +
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.
 
<pre>
 
<pre>
  +
:map <F3> :w !detex \| wc -w<CR>
:w
 
:!wc %
 
 
</pre>
 
</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.
   
  +
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.
==References==
 
*{{help|12.5}}
 
   
  +
==Counting occurrences of a pattern==
==Comments==
 
  +
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!
The code sample given above for using an external program, includes saving the
 
buffer to disk. To use an external program without having to save to disk use
 
the following:
 
   
  +
For example to count space delimited words in a file you could use:
 
<pre>
 
<pre>
 
:%s/[^ ]\+//gn
:w !wc
 
 
</pre>
 
</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}}
See {{help|:write_c}} for more details on the above code snippet.
 
   
  +
==Related plugins==
[[User:BenArmston|BenArmston]] 16:35, 5 March 2008 (UTC)
 
  +
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:
----
 
  +
<pre>
  +
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==
 
*{{help|12.5}}
  +
 
==Comments==

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