你如何列出Vim搜索的匹配?

Léo*_* 준영 96 vim search

当我点击时,我想列出比赛:

/example
Run Code Online (Sandbox Code Playgroud)

这样我就可以看到所有比赛的位置.

too*_*php 212

:g//p
Run Code Online (Sandbox Code Playgroud)

在更长的形式:

:global/regular-expression/print
Run Code Online (Sandbox Code Playgroud)

您可以省略模式/正则表达式,Vim将重复使用上一个搜索词.

花絮:grep的工具是这个命令序列命名.

  • `:g //` - 因为p(rint)是默认动作,你也可以把它留下来. (17认同)
  • `:g /`甚至更短! (5认同)
  • 是否有可能在这里看到上下文,比如grep`--context 9`? (2认同)

Tau*_*son 49

你也可以这样做:

g/pattern/#

这将打印您想要的图案和线的编号.

  • 好一个.但如果你已经启用了行号,则不需要`#`.它默认显示行号 (2认同)

Pau*_*nov 42

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

:vimgrep example %

要么

:grep example %

这将填充"错误清单"所有的比赛,这样就可以使用:copen它们全部列出在quickfix缓冲区,按特定的行中输入跳到那场比赛,或者使用命令状:cn,并:cp以来回走.

有关详细解释,请参阅我对类似问题的回复


fre*_*eeo 33

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

  • 不错的提示!这导致我使这个映射自动扩展光标下的单词并在一次扫描中运行:nnoremap <leader> f:lvim/<cr> = expand("<cword>")<cr> /%<cr>: LOPEN <CR> (2认同)

dek*_*ard 15

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

[I
Run Code Online (Sandbox Code Playgroud)

这将列出光标下所有出现的单词.它可能比您需要的多,因为它还将搜索当前文件中包含的任何文件.

但是这个命令的好处是搜索结果显示还显示了匹配数的计数,以及每个匹配的行号.

:help include-search
Run Code Online (Sandbox Code Playgroud)

看到很多变种.

关于

:g//p
Run Code Online (Sandbox Code Playgroud)

这可以进一步减少到

:g//
Run Code Online (Sandbox Code Playgroud)

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


Kev*_*vin 10

使用:set hlsearch将突出显示黄色的所有匹配,允许您轻松扫描文件以进行匹配.在搜索之后,这可能不是你想要的:g // p会给你列出的匹配

  • 应该是':set hlsearch'而不是':hlsearch'. (2认同)

use*_*400 8

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

/example
:g//p
Run Code Online (Sandbox Code Playgroud)

你也可以直接写

:g/example/p
Run Code Online (Sandbox Code Playgroud)

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

:g/example
Run Code Online (Sandbox Code Playgroud)

而不是p(rint),其他动作也是可能的,例如d(elete).请参阅:help:global


use*_*022 8

你可以得到一个漂亮的quickfix窗口,匹配形成你当前的搜索模式

:vim // %
:copen
Run Code Online (Sandbox Code Playgroud)

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

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

:bufdo vimgrepadd // %
:copen
Run Code Online (Sandbox Code Playgroud)


Sag*_*ain 5

g/pattern
Run Code Online (Sandbox Code Playgroud)

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

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

g/pattern/#
Run Code Online (Sandbox Code Playgroud)

将显示行号.


Yor*_*iev 5

    " 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
Run Code Online (Sandbox Code Playgroud)