2011-03-18 5 views
0

に印刷されています。開いているドキュメントのテキストを更新する関数を.vimrcファイルに追加したいと思っています。特に "ワードカウント:"という文字列を探します。現在の文書の単語数。ワードカウントはVimドキュメント

これは主にプログラミングの練習であり、vimをよりよく学ぶために、私はこの作業を行うためのwcのような外部プログラムがあることを知っています。誰かが私はそれがどこに単語数を挿入するようLastModifiedの機能に追加する方法を見つけ出す助けることはでき

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! LastModified() 
    if &modified 
    let save_cursor = getpos(".") 
    let n = min([15, line("$")]) 
    keepjumps exe '1,' . n . 's#^\(.\{,10}LOC:\).*#\1' . 
      \ ' ' . CountNonEmpty() . '#e' 
    call histdel('search', -1) 
    call setpos('.', save_cursor) 
    endif 
endfun 

autocmd BufWritePre * call LastModified() 

:ここ

は、私はコードの行数をカウントするために使用している同様の機能の一例ですヘッダーのテキストワード数を見つけますか?

答えて

1

もう少し掘り下げた後、私は答えを見つけました。この私が、私は他の誰が私の.vimrcのこの部分が有用であることを発見した場合にそれをここに組み込む方法を投稿しますFast word count function in Vim

に掲載のマイケル・ダン、別のStackOverflowのユーザーからのコードを、次のとおりです。

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 

autocmd BufWritePre * call LastModified() 
関連する問題