28

I know I can use wc for counting characters, words and lines of files at the command line.

Is there any way I can can count the number of words while in vim?

32

You can count words and lines inside vi using vi's own counter:

Press g and then CTRL-g. Then the bottom line look for example like this:

Col 1 of 11; Line 1 of 106; Word 1 of 344; Byte 1 of 2644

Or use vi's method to call shell commands:

:w !wc -w

This calls the save (:w) command first and then wc -w and shows the output. Example:

:w !wc -w
344

Press ENTER or type command to continue

Press Enter to go back to vi.

1
  • 1
    It is actually incorrect description of :w !<cmd> construct. It writes the current buffer to a pipe connected to the command. No separate write of the current buffer to a file is promised. Nevertheless, it does what question asked for. – mcepl Nov 17 '17 at 9:45
4

Since vim version 7.4.1042

Since vim version 7.4.1042, one can simply alter the statusline as follows:

set statusline+=%{wordcount().words}\ words
set laststatus=2    " enables the statusline.

Word count in vim-airline

Word count is provided standard by vim-airline for a number of file types, being at the time of writing: asciidoc, help, mail, markdown, org, rst, tex ,text

If word count is not shown in the vim-airline, more often this is due to an unrecognised file type. For example, at least for now, the compound file type markdown.pandoc is not being recognised by vim-airline for word count. This can easily be remedied by amending the .vimrc as follows:

let g:airline#extensions#wordcount#filetypes = '\vasciidoc|help|mail|markdown|markdown.pandoc|org|rst|tex|text'
set laststatus=2    " enables vim-airline.

The \v statement overrides the default g:airline#extensions#wordcount#filetypes variable. The last line ensures vim-airline is enabled.

In case of doubt, the &filetype of an opened file is returned upon issuing the following command:

:echo &filetype

Here is a meta-example:

vim-airline word count

1

You can also try for :!wc % in Vim, though it counts the size of the file on-disk, not what is in Vim's buffer. This may or may not be what you wanted.

3
  • This counts what’s on disk (if anything), not what’s in the buffer — so if you have changed the contents of the buffer since the last save, or not saved at all, you’ll get the wrong count (or even an error if you’re creating a file). – Stephen Kitt Mar 20 '19 at 8:57
  • Contra Stephen, this is a correct solution to "How can I count the number of words in a file whilst editing the file in vim", while the accepted answer strictly-speaking isn't (although it's ambiguous what was meant or whether it matters). – Michael Homer Mar 20 '19 at 9:02
  • Funny that it's similar (minus the -w flag to restrict the output to words only) to a delete answer from 2014 by D_Bye (who apparently self-deleted it). – Jeff Schaller Mar 20 '19 at 10:20
1

For those who want to count number of words in a given piece of text (not whole file), use \S\+ regexp.

  1. Select the text of interest (visual mode)
  2. :s/\S\+//gn

Result. Vim will show you something like this: 10 matches on 1 line

When you hit : in visual mode, vim prepends your command with '<,'> which means to apply the command against the selected text.

\S\+ captures words, that is groups of characters separated by whitespace.

0

Plugins such as vim-airline can provide word counts for a file (and selections) as part of a status bar.

2
  • This answer isn't link-only. I didn't include the specifics of any particular plugin, because (as you say) particular plugins might become outdated. – Patrick Sanan Jan 9 '19 at 19:04
  • I stand corrected. However I would say in the future you should try to include relevant steps to implement the solution you are presenting. Thank you! – kemotep Jan 9 '19 at 19:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.