Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何计算Vim中的搜索结果?_Vim - Fatal编程技术网

如何计算Vim中的搜索结果?

如何计算Vim中的搜索结果?,vim,Vim,如何使用Vim在文本文件中查找特定单词的重复次数 例如,我想看看单词“name”在代码中重复了多少次: "collection": "animals", "fields": [ { "field": "id", "name": "" }, { "field": "age", "name": "" }, { "field": "origin",

如何使用Vim在文本文件中查找特定单词的重复次数

例如,我想看看单词“name”在代码中重复了多少次:

"collection": "animals",
    "fields": [
      {
        "field": "id",
        "name": ""
      },
      {
        "field": "age",
        "name": ""
      },
      {
        "field": "origin",
        "name": ""
      }

结果-3

您可以使用替换命令中的
n
标志计算匹配数。使用以下命令显示某个单词与当前缓冲区中的文本匹配的次数:

:%s/some-word//gn

您可以阅读

上的所有详细信息。我有一个计数字功能,您可以通过多种方式使用它

fun! CountWordFunction()
    try
        let l:win_view = winsaveview()
        exec "%s/" . expand("<cword>") . "//gn"
    finally
        call winrestview(l:win_view)
    endtry
endfun
command! -nargs=0 CountWord :call CountWordFunction()
有趣!CountWordFunction()
尝试
让l:win_view=winsaveview()
执行代码“%s/”。展开(“”)。“//gn”
最后
调用winrestview(l:win_视图)
末日
结束
命令-nargs=0 CountWord:调用CountWordFunction()
因此,您可以映射它:

nnoremap <F3> :CountWord<CR>
nnoremap:CountWord
甚至鼠标双击

" When double click a word vim will hightlight all other ocurences
" see CountWordFunction()
" [I shows lines with word under the cursor
nnoremap <silent> <2-LeftMouse> :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:CountWord<cr>
nnoremap <Leader>* :let @/='\V\<'.escape(expand('<cword>'), '\').'\>'<cr>:set hls<cr>:CountWord<cr>
“当双击一个单词时,vim将高亮显示所有其他眼睛
“请参阅CountWordFunction()
“[我显示光标下有单词的行
nnoremap:let@/='\V\':设置hls:CountWord
nnoremap*:let@/='\V\':设置hls:CountWord
我愿意

:g/pattern/d
这将在左下角显示被删除的次数

然后,您可以通过
u

撤消删除,My将打印以下输出(光标位于第一个匹配项上,并使用
映射或
:SearchPosition name
命令):

此行中的鞋底匹配,以下行中的2在5,13范围内/\/
它建立在这里给出的其他解决方案的基础上,并且是为了方便地获得此类统计数据而专门编写的。

Nowdays(意思是Vim)您可以使用:

set shortmess-=S
要启用每次执行
/
搜索时显示在右下角的
[x/y]
计数

请注意,如果搜索结果超过99个,Vim很不幸只显示
[x/>99]

出于这个原因,我个人使用,它适用于任何数量的结果:


(默认情况下,插件仅限于“仅”少于100k行的文件,这可以通过以下命令进行调整:
let g:searchindex_line_limit=1000000

  • 每行有多个匹配项
  • g
    代表全局,它将匹配每行的多个引用

    输出如图所示

    13 matches on 10 lines
    
    10 matches on 10 lines
    
  • 线匹配
  • 输出如图所示

    13 matches on 10 lines
    
    10 matches on 10 lines
    

    -
    有什么作用?它从设置中删除了S标志。@ruohola您使用的是哪种配色方案?@NicolasLykkeIversen with
    set background=dark
    配色方案solarized8
    @ruohola谢谢。我想我用过一次,但在macOS上使用Terminal.app看起来不太好
    10 matches on 10 lines