Fast word count function in Vim

后端 未结 16 2043
后悔当初
后悔当初 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条回答
  •  温柔的废话
    2020-12-08 01:25

    This will recalculate the number of words whenever you stop typing for a while (specifically, updatetime ms).

    let g:word_count=""
    fun! WordCount()
        return g:word_count
    endfun
    fun! UpdateWordCount()
        let s = system("wc -w ".expand("%p"))
        let parts = split(s, ' ')
        if len(parts) > 1
            let g:word_count = parts[0]
        endif
    endfun
    
    augroup WordCounter
        au! CursorHold * call UpdateWordCount()
        au! CursorHoldI * call UpdateWordCount()
    augroup END
    
    " how eager are you? (default is 4000 ms)
    set updatetime=500
    
    " modify as you please...
    set statusline=%{WordCount()}\ words
    

    Enjoy!

提交回复
热议问题