April 16, 2023

Show word count of a text file from the command line with wc or pandoc & lua

Table of contents:

  • Show word count with the programme wc in the terminal
  • Show word count with Pandoc and a lua script
  • Show word count with the programme wc in Vim

Show word count with the programme wc in the terminal

wc is a programme to count words, characters and lines, already installed with most, if not all, Unix-based operating systems.

Open the terminal (on Trisquel GNU/Linux, you can use the shortcut alt + F2; then type terminal; the terminal programme should come up).

Once the terminal is open run:

$ wc file.txt

($ represents the prompt of your terminal.)

You should see three numbers followed by the name of your file, for instance:

24  488 2791 file.txt

The first number is the number of lines, the second the number of words and third the number of characters.

If you only want to display the number of words, invoke the option -w, short for –words, for example:

$ wc -w file.txt

This command should only display the number of words:

488 file.txt

That is it. If you wish to find out more about wc you can consult the (short) manual by running $ man wc in a terminal.

wc is published under the GNU General Public Licence version 3 or later.

Show word count with Pandoc and a lua script

If you write using Markdown (or another markup language maybe) and want to count words only, i.e. exclude comments for instance, you can use Pandoc with lua script. Conversely, wc will count all text, including text that is commented out.

First download Pandoc, on Trisquel GNU/Linux run:

$ sudo apt install pandoc

Then download the lua script wordcount.lua (there is a copy of the script hosted on this website), run:

$ wget https://yctct.com/wordcount.lua

Then, to count words of a file run:

$ pandoc file.txt --lua-filter=path/to/file/wordcount.lua

If wordcount.lua is located in the same directory as the file you wish to count words of, you can run:

$ pandoc file.txt --lua-filter=wordcount.lua

The terminal should print something like:

449 words in body
2114 characters in body
2551 characters in body (including spaces)

You can notice that the lua script counts less words and characters than wc for the same file.
That is because I have a paragraph commented out. With Markdown, you can comment out text like this: .

Show word count with the programme wc in Vim

To display word count of a file in Vim, write it to the programme wc:

:w !wc  -w

At the bottom of the buffer, Vim should print the number of words, for example:

488
Press ENTER or type command to continue

By the way, in Vim, ! is used to call an external programme.

To display word count of a file in Vim with Pandoc and the lua filter wordcount.lua run:

:w !pandoc --lua-filter=path/to/script/wordcount.lua

At the bottom of the buffer, Vim should print something like this:

449 words in body
2114 characters in body
2551 characters in body (including spaces)
Press ENTER or type command to continue

That is it.

If you don’t use Pandoc yet and want to get started, you can read: https://yctct.com/use-pandoc

Similarly, if you want to get started with Vim, you can read: https://yctct.com/use-vim

You might also be interested in a guide I put together: A guide to simple and stupid usage of computers for writers (anyone who writes really). This is published under a BY-NC-SA licence, meaning you can print it and distribute it to your peers freely.


command-line interface (cli) personal computing wc pandoc trisquel GNU linux vim text processing office applications wiki shell literacy

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88