【问题标题】:How can you list the matches of Vim's search?你如何列出 Vim 搜索的匹配项?
【发布时间】:2010-10-05 07:30:27
【问题描述】:

我想列出匹配项,当我点击时:

/example

这样我就可以同时看到所有匹配项的位置。

【问题讨论】:

    标签: search vim


    【解决方案1】:
    :g//p
    

    更长的形式:

    :global/regular-expression/print
    

    你可以省略模式/正则表达式,Vim 会重新使用之前的搜索词。

    琐事:grep 工具就是以此命令序列命名的。

    【讨论】:

    • :g// - 因为 p(rint) 是默认操作,您也可以省略它。
    • :g/ 更短!
    • 是否可以在这里查看上下文,例如 grep --context 9
    • @VitalyZdanevich 试试:%s/regular-expression//gn 它会产生一些类似的东西:X matches on Y lines
    • @AlexejMagura 我的意思是 - 如何查看字符串前后的几行?
    【解决方案2】:

    你也可以这样做:

    g/pattern/#

    这将打印您想要的模式和行号。

    【讨论】:

    • 不错的一个。但是如果您已经启用了行号,则不需要#。默认显示行号
    【解决方案3】:

    如果您想查看此列表并在匹配之间快速跳转,请考虑使用

    :vimgrep example %

    :grep example %

    这将使用所有匹配项填充“错误列表”,以便您可以使用:copen 将它们全部列出在快速修复缓冲区中,在特定行上按 Enter 键以跳转到该匹配项,或使用诸如 @ 之类的命令987654325@和:cp来回走。

    详细解释请见my reply to a similar question

    【讨论】:

    • 这在 MacVim 8.1 上给出了E499: Empty file name for '%' or '#', only works with ":p:h"
    【解决方案4】:

    刚学会一个新的:Location List
    键入:lvim foo % 以在当前文件中搜索foo,并将包含foo 的所有匹配项输入到位置列表
    键入:lopen 以在快速修复窗口中打开位置列表,该窗口可以像往常一样完全导航。
    使用 :lnext/:lprevious 来遍历列表(使用 tpope/unimpaired 映射以获得最佳体验)

    【讨论】:

    • 不错的提示!这导致我创建了这个映射,它自动扩展光标下的单词并在一次扫描中打开:nnoremap f :lvim /=expand("")/ %: lopen
    【解决方案5】:

    另一种可能是使用包含文件搜索命令。

    [I
    

    这将列出光标下所有出现的单词。但它可能超出您的需要,因为它还会搜索当前文件中包含的所有文件。

    但是这个命令的好处是搜索结果显示除了每个匹配的行号之外,还显示匹配数的计数。

    :help include-search
    

    查看很多变体。

    关于

    :g//p
    

    这可以进一步简化为

    :g//
    

    因为正如其他人所说,p(rint) 是默认操作。

    【讨论】:

    • 是否有可能从 [I 获得带有上下文的结果,例如在 grep --context 9 上?
    【解决方案6】:

    您可以获得一个漂亮的quickfix 窗口,其中包含您当前搜索模式的匹配项

    :vim // %
    :copen
    

    如果您之前仅使用 /pattern 制作了复杂的搜索模式,则非常方便

    编辑:刚刚发现这也适用于所有打开的缓冲区

    :bufdo vimgrepadd // %
    :copen
    

    【讨论】:

      【解决方案7】:

      使用:set hlsearch 将以黄色突出显示所有匹配项,使您可以轻松扫描文件以查找匹配项。这可能不是你想要的,但搜索后,:g//p 会给你列出的匹配项

      【讨论】:

      • 应该是 ':set hlsearch' 而不是 ':hlsearch'。
      • 映射:nohl 以在您不再需要高亮时清除它们很有用。
      【解决方案8】:

      详细说明这个...而不是

      /example
      :g//p
      

      也可以直接写

      :g/example/p
      

      或者,因为 p(rint) 是 :g(lobal) 命令的默认操作,所以可以缩短为

      :g/example
      

      除了 p(rint),其他操作也是可能的,例如删除)。请参阅 :help :global

      【讨论】:

        【解决方案9】:
        g/pattern
        

        如果你有:set number,上面的命令也会显示行号。

        如果你还没有:set number,那么

        g/pattern/#
        

        将显示行号。

        【讨论】:

          【解决方案10】:
              " put in your ~/.vimrc file
              " START search related configs and helps
              "
              " ignore case when searching
              set ignorecase
          
              " search as characters are entered, as you type in more characters, the search is refined
              set incsearch
          
              " highlight matches, in normal mode try typing * or even g* when cursor on string
              set hlsearch
          
              " yank those cheat commands, in normal mode type q: than p to paste in the opened cmdline
              " how-to search for a string recursively
              " :grep! "\<doLogErrorMsg\>" . -r
              "
              " how-to search recursively , omit log and git files
              " :vimgrep /srch/ `find . -type f \| grep -v .git \| grep -v .log`
              " :vimgrep /srch/ `find . -type f -name '*.pm' -o -name '*.pl'`
              "
              " how-to search for the "srch" from the current dir recursively in the shell
              " vim -c ':vimgrep /srch/ `find . -type f \| grep -v .git \| grep -v .log`'
              "
              " how-to highlight the after the search the searchable string
              " in normmal mode press g* when the cursor is on a matched string
          
              " how-to jump between the search matches - open the quick fix window by
              " :copen 22
          
              " how-to to close the quick fix window
              " :ccl
          
              " F5 will find the next occurrence after vimgrep
              map <F5> :cp!<CR>
          
              " F6 will find the previous occurrence after vimgrep
              map <F6> :cn!<CR>
          
              " F8 search for word under the cursor recursively , :copen , to close -> :ccl
              nnoremap <F8> :grep! "\<<cword>\>" . -r<CR>:copen 33<CR>
          
              " omit a dir from all searches to perform globally
              set wildignore+=**/node_modules/**
          
              " use perl regexes - src: http://andrewradev.com/2011/05/08/vim-regexes/
              noremap / /\v
              "
              " STOP  search related configs and helps
          

          【讨论】:

          • 我非常喜欢你的 F5-F8 创意。在这里有这些命令的一些可视化/输出会很好。 F8 是如何设计的?它只搜索当前目录? - - 省略 dir 是一种习惯 - - 为什么是 node_modules? - - perl 正则表达式如何改变 Vim 的默认功能? - - 以下是我如何设计我的 .tex 文件搜索 askubuntu.com/a/789769/25388 很高兴向您学习如何改进它。它不是那么直观,所以我自己也经常忘记逻辑。
          • node_modules 是包含大量来自 npm 模块的 js 代码的目录,如果你要开发前端的话……这只是一个例子……
          • 备份你的 .vimrc 并添加这些设置,用 :so % 加载它们,然后尝试在命令中运行“帮助”...... vim 很难 - 你必须重复那些成千上万次,但是一旦你掌握了它们,你会笑@每个指出他们伟大的新 UI IDE 功能的人......
          【解决方案11】:

          我为此编写了一段代码。它实际上避免了vimgrep 中的问题。它甚至适用于未命名的文件。而且更容易使用。

          function! Matches(pat)
              let buffer=bufnr("") "current buffer number
              let b:lines=[]
              execute ":%g/" . a:pat . "/let b:lines+=[{'bufnr':" . 'buffer' . ", 'lnum':" . "line('.')" . ", 'text': escape(getline('.'),'\"')}]"
              call setloclist(0, [], ' ', {'items': b:lines}) 
              lopen
          endfunction
          

          当您使用模式调用它时,它会打开包含所有匹配项的位置窗口。

          这可能是一个命令

          command! -nargs=1 Mat call Matches(<f-args>)
          

          所以你需要做的就是输入:Mat pattern

          我还使用以下映射来获取当前视觉选择的匹配项。

          vnoremap Y "xy:call Matches(@x)<CR>
          

          【讨论】:

            【解决方案12】:

            Ctrl-f 列出所有搜索结果:

            nmap <C-f> :vimgrep /<C-r>//g %<CR> \| !:copen <Enter>
            

            【讨论】:

              猜你喜欢
              • 2010-09-11
              • 2011-09-11
              • 1970-01-01
              • 2013-08-13
              • 2010-10-19
              • 1970-01-01
              • 2014-07-10
              • 2011-11-12
              • 1970-01-01
              相关资源
              最近更新 更多