Get live word count for document in vim

Assuming your status line is enabled (set laststatus=2), the following:

set statusline+=%{wordcount().words}\ words

does exactly what you want in Vim version 7.4.1042 and above:

wc

See :help wordcount().


If you absolutely need backward compatibility, the following is pretty much guaranteed to work in Vim 7.x, and will probably also work in earlier versions:

function! WC()
    return len(split(join(getline(1,'$'), ' '), '\s\+'))
endfunction
set statusline+=%{WC()}\ words

Some of the answers from those old threads may be faster or smarter, though.


Your comments about those functions from those old threads not changing anything to your status line make me wonder if the problem is in all those old answers or elsewhere. Maybe… you don't have a status line to begin with?

Tags:

Vim

Word Count