Live Word Count in Vim

19 May 2015

As a distraction free writing environment, vim has worked great for me in the past. And using sites like 750words.com leads to me being more distracted. It’s far easier for me to full-screen vim and use that as a distraction-free environment. 750words.com has one thing that I like - A live word count in the footer of the page, so I know when to stop. I did some googling around and here’s what I found on stackoverflow for doing something similar in vim.

Add the following to your .vimrc

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

And then, if you’re using ftplugin, add this to .vim/ftplugin/markdown.vim

setlocal statusline=wc:%{WordCount()}

This will give you a small counter at the bottom with the word count, like so.

Word Count in vim. See bottom left corner

All Posts ⇉

Comments

blog comments powered by Disqus