【发布时间】:2010-10-05 07:30:27
【问题描述】:
我想列出匹配项,当我点击时:
/example
这样我就可以同时看到所有匹配项的位置。
【问题讨论】:
我想列出匹配项,当我点击时:
/example
这样我就可以同时看到所有匹配项的位置。
【问题讨论】:
【讨论】:
:g// - 因为 p(rint) 是默认操作,您也可以省略它。
:g/ 更短!
--context 9?
:%s/regular-expression//gn 它会产生一些类似的东西:X matches on Y lines
你也可以这样做:
g/pattern/#
这将打印您想要的模式和行号。
【讨论】:
#。默认显示行号
如果您想查看此列表并在匹配之间快速跳转,请考虑使用
:vimgrep example %
或
:grep example %
这将使用所有匹配项填充“错误列表”,以便您可以使用:copen 将它们全部列出在快速修复缓冲区中,在特定行上按 Enter 键以跳转到该匹配项,或使用诸如 @ 之类的命令987654325@和:cp来回走。
【讨论】:
E499: Empty file name for '%' or '#', only works with ":p:h"
刚学会一个新的:Location List!
键入:lvim foo % 以在当前文件中搜索foo,并将包含foo 的所有匹配项输入到位置列表。
键入:lopen 以在快速修复窗口中打开位置列表,该窗口可以像往常一样完全导航。
使用 :lnext/:lprevious 来遍历列表(使用 tpope/unimpaired 映射以获得最佳体验)
【讨论】:
另一种可能是使用包含文件搜索命令。
[I
这将列出光标下所有出现的单词。但它可能超出您的需要,因为它还会搜索当前文件中包含的所有文件。
但是这个命令的好处是搜索结果显示除了每个匹配的行号之外,还显示匹配数的计数。
:help include-search
查看很多变体。
关于
:g//p
这可以进一步简化为
:g//
因为正如其他人所说,p(rint) 是默认操作。
【讨论】:
[I 获得带有上下文的结果,例如在 grep --context 9 上?
您可以获得一个漂亮的quickfix 窗口,其中包含您当前搜索模式的匹配项
:vim // %
:copen
如果您之前仅使用 /pattern 制作了复杂的搜索模式,则非常方便
编辑:刚刚发现这也适用于所有打开的缓冲区
:bufdo vimgrepadd // %
:copen
【讨论】:
使用:set hlsearch 将以黄色突出显示所有匹配项,使您可以轻松扫描文件以查找匹配项。这可能不是你想要的,但搜索后,:g//p 会给你列出的匹配项
【讨论】:
:nohl 以在您不再需要高亮时清除它们很有用。
详细说明这个...而不是
/example
:g//p
也可以直接写
:g/example/p
或者,因为 p(rint) 是 :g(lobal) 命令的默认操作,所以可以缩短为
:g/example
除了 p(rint),其他操作也是可能的,例如删除)。请参阅 :help :global
【讨论】:
g/pattern
如果你有:set number,上面的命令也会显示行号。
如果你还没有:set number,那么
g/pattern/#
将显示行号。
【讨论】:
" 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
【讨论】:
node_modules? - - perl 正则表达式如何改变 Vim 的默认功能? - - 以下是我如何设计我的 .tex 文件搜索 askubuntu.com/a/789769/25388 很高兴向您学习如何改进它。它不是那么直观,所以我自己也经常忘记逻辑。
我为此编写了一段代码。它实际上避免了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>
【讨论】:
Ctrl-f 列出所有搜索结果:
nmap <C-f> :vimgrep /<C-r>//g %<CR> \| !:copen <Enter>
【讨论】: