vim

Vim Word Count

How can we find out how many words are in a file? Or the number of lines or even characters? It’s simple to count the overall number of words, the number of times a certain term appears, and so on. You have at least two possibilities for counting words. Using the “!wc” percent shell tool or by pressing the “g” and “Ctrl+g”. The first uses the current file (buffer) as a standard input in our example. Of course, the scope can be fine-tuned to include the entire file or only a portion of it. To begin, use vim rather than vi. Vim is installed by default on Linux and OS X, and typing vi launches vim. However, to get the modernized version on OpenBSD, Solaris, and other Unix systems, you may need to install a package and run the vim command specifically.

Count Number of Words in Vim

For counting the number of words, press g, then Ctrl+g. This will display the number of words currently in the buffer. Here we have a sample file with some text on which we are going to perform our function. As you can see, when we press g, it is displayed on the right bottom. Once you see “g” press “Ctrl+g” to obtain the output seen in the second screenshot.

You will see that the text detail is visible at the bottom. It shows columns, lines, words, and byte information.

To count the number of words in a given text block, select it, then hit g and Ctrl-g to count the words. If you wish, you can use the ” % ” filename expansion to call an external program. For instance, on Unix, you could do the following to determine the total number of words in a file.

Press:w and get the following output. It shows that the file has 7 lines and 145 characters, as you can notice in the screenshot below.

But instead of :w if we give command :!wc %, you will see that it will show you all the details of words and characters along with the file name (filenew is the name of the file in our case). Here the initial:w command writes the current file to a file that an external program can read. Use this command to save the buffer without saving it to a file as an option.

Live Word Count in Vim

It’s a little tough to obtain a live word count on the vim status bar, but it’s only necessary once. You can change your vimrc file to display the word count in the status line at all times. In Vim versions 7.4.1042 and higher, the following line of code achieves exactly what you want if your status line is enabled (set laststatus=2).

You can define what counts as a “word” in Vim to your liking. Those who use Vim to edit raw HTML pages, for example, might want to change the definition such that only words displayed by a web browser are considered words, but none of the formatting tags are.

Count Number of Printed words (LaTeX Document)

If you’re using LaTeX and need a printed word count (for example, for a journal paper), wc’s output won’t help you because it counts all markup and non-printed aspects of the file. Use a program like detex to remove the markup from the text before piping it to wc on Windows. Unxutils or Cygwin will appeal to Windows users who require utilities that *nix users take for granted, such as wc. Below an example of mapping to F3 is shown. The current buffer is sent to detex’s stdin, which then writes the detex’d file to wc’s stdin. If something is pickedin visual, visual-line, or visual-block mode, only the selection is considered; otherwise, the entire buffer is counted.

Instead of opening a console window each time, the output of wc’s can be returned to a discrete error message at the bottom of the buffer or window in Windows, which will make it less obvious.

Note: The Texcount Perl script, which is a very intelligent piece of software, can also be used for this purpose, but it does not count everything precisely.

Count Occurrences of a Pattern in Vim

Use the :s substitution command with the n argument to count the number of times a pattern appears (counts number of hits; no changes are made). Consider the code in the next section. You can easily count the occurrences in a part of a file by combining this with the Search and Replace in a visual selection. To count space-delimited words in a file, for example, use the following line of code.

Here you can see the output after executing the above command.

While the cursor is above a word, [I] is a shortcut that can list all instances of that term (which displays each line that contains the current keyword in this file and included files when using a programming language such as C).

Count Occurrences of a Word

In command mode, use the following to find instances of a phrase in a document with vim. Then you can use n and shift+n to go forward and backward through all instances of this phrase.

# /the-term-that-you-want-to-search

For example, if we search the word “mode” in our file. Refer to the following screenshot to see how it looks like. It has highlighted the word for you.

This will not, however, count the total number of instances of the term, for which there is a simple option, which can also be used in command mode:

This will yield the overall count for this search by utilizing the replace command, which counts the number of matches with n and allows multiple matches per line with g rather than replacing the search phrase.

Conclusion

The topic of this article was vim word count. We’ve provided instructions on how to count the number of words in a file here. In addition, you’ve learned how to count the number of words in various settings. Additionally, we learned how to count the number of printed words in a LaTeX document.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.