Vim 中的快速字数统计功能 [英] Fast word count function in Vim

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

问题描述

我正在尝试在 vim 状态行中显示实时字数.我通过在我的 .vimrc 中设置我的状态行并在其中插入一个函数来做到这一点.这个函数的思想是返回当前缓冲区中的字数.这个数字然后显示在状态行上.这应该很好地工作,因为状态行几乎在每一个可能的机会都会更新,因此计数将始终保持实时"状态.

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 is to return the number of words in the current buffer. This number is then displayed on the status line. This should work nicely as the statusline is updated at just about every possible opportunity so the count will always remain 'live'.

问题是我当前定义的函数很慢,所以vim在用于除最小文件之外的所有文件时显然很慢;因为这个函数执行的太频繁了.

The problem is that the function I have currently defined is slow and so vim is obviously sluggish when it is used for all but the smallest files; due to this function being executed so frequently.

总而言之,有没有人有一个聪明的技巧来生成一个函数,该函数在计算当前缓冲区中的单词数并返回结果时速度极快?

In summary, does anyone have a clever trick for producing a function that is blazingly fast at calculating the number of words in the current buffer and returning the result?

推荐答案

这是 Rodrigo Queiro 想法的可用版本.它不会改变状态栏,而是恢复 statusmsg 变量.

Here's a usable version of Rodrigo Queiro's idea. It doesn't change the status bar, and it restores the statusmsg variable.

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

这似乎足够快,可以直接包含在状态行中,例如:

This seems to be fast enough to include directly in the status line, e.g.:

:set statusline=wc:%{WordCount()}

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

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