Fast word count function in Vim

后端 未结 16 2081
后悔当初
后悔当初 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:06

    My suggestion:

    function! UpdateWordCount()
      let b:word_count = eval(join(map(getline("1", "$"), "len(split(v:val, '\\s\\+'))"), "+"))
    endfunction
    
    augroup UpdateWordCount
      au!
      autocmd BufRead,BufNewFile,BufEnter,CursorHold,CursorHoldI,InsertEnter,InsertLeave * call UpdateWordCount()
    augroup END
    
    let &statusline='wc:%{get(b:, "word_count", 0)}'
    

    I'm not sure how this compares in speed to some of the other solutions, but it's certainly a lot simpler than most.

提交回复
热议问题