在vim中获取文档的实时字数

The*_*era 5 vim word-count

我希望vim在状态栏中显示总文档字数(显示当前行和字符编号).我在SO上遇到过类似的问题,并尝试了这里这里提到的所有建议- 但是我们的状态栏中没有任何一个有任何影响.

为了明确地命名一些,我尝试在我的~/.vimrc(以及随后重新启动的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:\).*#\1' .
          \ ' ' . CountNonEmpty() . '#e'
    keepjumps exe '1,' . n . 's#^\(.\{,10}Word Count:\).*#\1' .
          \ ' ' . WordCount() . '#e'
    call histdel('search', -1)
    call setpos('.', save_cursor)
  endif
endfun 
Run Code Online (Sandbox Code Playgroud)

要么

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()}
Run Code Online (Sandbox Code Playgroud)

要么

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()}
Run Code Online (Sandbox Code Playgroud)

要么

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
Run Code Online (Sandbox Code Playgroud)

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

rom*_*inl 13

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

set statusline+=%{wordcount().words}\ words
Run Code Online (Sandbox Code Playgroud)

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

厕所

:help wordcount().


如果你绝对需要向后兼容性,那么以下几乎可以保证在Vim 7.x中工作,并且可能也适用于早期版本:

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

但是,那些旧线程的一些答案可能更快或更聪明.


您对那些旧线程中的那些函数的评论没有改变到您的状态行,这让我想知道问题是出现在所有那些旧答案中还是其他地方.也许......你没有开始的状态行?