How to compute the number of times word appeared in a file or in some range

自作多情 提交于 2019-12-09 08:14:11

问题


Sometimes I want to see how many times a certain function is called in a file or a code block. How do you do that? I am using Vim 7.2.

I presume you have to use !wc or some such.


回答1:


For counting the number of times some pattern occurs, use:

:%s/pattern//gn

The 'n' flag count the number of occurrences without doing any change to the document.

For counting the total number of words, you have several options.

If you want to run as an external command:

:!wc -w %

If you want to run it inside VIM:

:w !wc -w



回答2:


You can use a substitution without a replacement to get the number of occurences. If you want to count all occurences of word use

:%s/\<word\>//gn

\< and \> match start and end of word. The n option prevents the substitution from being done.



来源:https://stackoverflow.com/questions/1398989/how-to-compute-the-number-of-times-word-appeared-in-a-file-or-in-some-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!