Vim文档中打印的字数 [英] Word Count printed in Vim document

查看:29
本文介绍了Vim文档中打印的字数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的.vimrc文件中添加一个函数,该函数更新打开的文档中的文本,特别是在找到文本"Word Count:"的地方,它将使用vim在当前文档中插入准确的字数

I'd like to add to my .vimrc file a function which updates the text in an open document, specifically where it finds the text "Word Count: " it would use vim to insert an accurate word count in the current document.

这主要是作为编程练习,并且为了更好地学习vim,我知道可以使用诸如wc之类的外部程序来完成这项工作.

This is mostly as a programming exercise and to better learn vim, I know there are external programs like wc available to do this work.

这是我用来计数代码行的类似功能的示例:

Here's an example of a similar function I'm using to count lines of code:

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()

有人可以帮我弄清楚如何添加到LastModified函数中,以便在标题中找到单词Word Count的地方插入单词计数吗?

Can someone help me figure out how to add to the LastModified function so that it inserts a word count where it finds the text Word Count in the header?

推荐答案

再进行一些挖掘之后,我找到了答案.这是另一个StackOverflow用户Michael Dunn的代码,发布在 Vim中的快速字数统计功能

After some more digging I found the answer. This is code from Michael Dunn, another StackOverflow user, posted at Fast word count function in Vim

如果其他人发现我的.vimrc的这一部分有用,我将在这里发布如何将其合并:

I'll post how I incorporated it here in case anyone else finds this portion of my .vimrc to be useful:

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()

这篇关于Vim文档中打印的字数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆