This was a result of using vim to write my essays. I started writing them in notes (vim-notes plugin) files and wanted vim-airline to display a live word count.

The Easy Way

As of this version of vim-airline’s wordcount plugin.

In your vimrc, add:

" Enable wordcount
let g:airline#extensions#wordcount#enabled = 1
" Add notes to filetypes
let g:airline#extensions#wordcount#filetypes = '\vnotes|help|markdown|rst|org|text|asciidoc|tex|mail'

By default, files of the type help|markdown|rst|org|text|asciidoc|tex|mail run the wordcount plugin.

The Complicated Way

Note: When I came up with this, I was really frustrated because I could not figure out `The Easy Way’ – the wordcount plugin went through a few re-writes that left the documented format of expressing filetypes outdated. So I decided to figure out how to write it by myself. Of course, I would figure out the easy way after I finished documenting the complicated way. Go Vivian.

normal mode word
count visual mode word count

You can get word count by typing g Ctrl+g in normal mode, or in visual mode (which will tell you how many words are selected).

First, I define a function that grabs the word count using the above command. First, it saves the position of the cursor. Then, it parses v:statusmsg by looking at whether you’re in Visual mode or not.

function! WordCount()
    let position = getpos(".")
    exe ":silent normal g\<c-g>"
    let stat = v:statusmsg
    let s:word_count = 0
    if stat != '--No lines in buffer--'
        if mode() == "V"
            let s:word_count = str2nr(split(stat)[5])
        else
            let s:word_count = str2nr(split(stat)[11])
        endif
    endif
    call setpos('.', position)
    return s:word_count
endfunction

This is how I made it show up in airline for files of the type note. Note that vim-airline automatically handles this for some set of default filetypes (see The Easy Way).

function! AirlineWC(...)
    if &filetype == 'notes'
        let w:airline_section_b = '%{WordCount()} words'
    endif
endfunction
call airline#add_statusline_func('AirlineWC')
back up