Fast word count function in Vim

后端 未结 16 2038
后悔当初
后悔当初 2020-12-08 00:27

I am trying to display a live word count in the vim statusline. I do this by setting my status line in my .vimrc and inserting a function into it. The idea of this function

16条回答
  •  -上瘾入骨i
    2020-12-08 01:18

    I really like Michael Dunn's answer above but I found that when I was editing it was causing me to be unable to access the last column. So I have a minor change for the function:

    function! WordCount()
       let s:old_status = v:statusmsg
       let position = getpos(".")
       exe ":silent normal 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
    

    I've included it in my status line without any issues:

    :set statusline=wc:%{WordCount()}

提交回复
热议问题