获取 vim 中文档的实时字数统计

Get live word count for document in vim

我希望vim在状态栏(显示当前行和字符数)中显示文档总字数。我在 SO 上遇到过类似的问题,并尝试了提到的所有建议 here and here --- 其中 none 对我的状态栏有任何影响。

为了明确列举一些,我尝试将以下任何内容粘贴到我的 ~/.vimrc 中(并且 ofc 随后重新启动 vim):

function! CountNonEmpty()
    let l = 1
    let char_count = 0
    while l <= line("$")
        if len(substitute(getline(l), '\s', '', 'g')) > 3   
            let char_count += 1 
        endif
        let l += 1
    endwhile
    return char_count
endfunction

function WordCount()
  let s:old_status = v:statusmsg
  exe "silent normal g\<c-g>"
  let s:word_count = str2nr(split(v:statusmsg)[11])
  let v:statusmsg = s:old_status
  return s:word_count
endfunction  

" If buffer modified, update any 'Last modified: ' in the first 20 lines.
" 'Last modified: ' can have up to 10 characters before (they are retained).
" Restores cursor and window position using save_cursor variable.
function! LastModified()
  if &modified
    let save_cursor = getpos(".")
    let n = min([15, line("$")])
    keepjumps exe '1,' . n . 's#^\(.\{,10}LOC:\).*#' .
          \ ' ' . CountNonEmpty() . '#e'
    keepjumps exe '1,' . n . 's#^\(.\{,10}Word Count:\).*#' .
          \ ' ' . WordCount() . '#e'
    call histdel('search', -1)
    call setpos('.', save_cursor)
  endif
endfun 

function WordCount()
  let s:old_status = v:statusmsg
  exe "silent normal g\<c-g>"
  let s:word_count = str2nr(split(v:statusmsg)[11])
  let v:statusmsg = s:old_status
  return s:word_count
endfunction
set statusline=wc:%{WordCount()}

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
set statusline=wc:%{WordCount()}

let g:word_count="<unknown>"
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

或更多。正如我所说,没有效果。没有错误消息,没有视觉上可察觉的变化。我想可能有一个我遗漏的常见问题,但它是什么?

假设您的状态行已启用 (set laststatus=2),则如下:

set statusline+=%{wordcount().words}\ words

在 Vim 7.4.1042 及更高版本中完全符合您的要求:

:help wordcount()


如果您绝对需要向后兼容,以下内容几乎可以保证在 Vim 7.x 中工作,并且可能也可以在早期版本中工作:

function! WC()
    return len(split(join(getline(1,'$'), ' '), '\s\+'))
endfunction
set statusline+=%{WC()}\ words

不过,那些旧话题的一些答案 可能 更快或更聪明。


你对那些旧线程中的那些功能的评论没有改变你的状态行让我想知道问题是在所有那些旧答案中还是在其他地方。也许……您没有状态行开头?

字数 vim-airline

vim-airline 为多种文件类型提供了标准字数统计,在撰写本文时为: asciidoc, help, mail, markdown, org, rst, tex ,text

如果 vim-airline 中未显示字数,这通常是由于无法识别的文件类型。例如,at least for now, the compound file type markdown.pandoc 未被 vim-airline 识别为字数统计。这可以通过如下修改 .vimrc 轻松解决:

let g:airline#extensions#wordcount#filetypes = '\vasciidoc|help|mail|markdown|markdown.pandoc|org|rst|tex|text'
set laststatus=2    " enables vim-airline.

\v 语句覆盖默认的 g:airline#extensions#wordcount#filetypes 变量。最后一行确保 vim-airline 已启用。

如有疑问,已打开文件的 &filetype 会在发出以下命令时返回:

:echo &filetype

这是一个元示例: