使用记事本++搜索字符串时,会打开新窗口并显示查找结果。我想在vim中使用这个功能。经过谷歌搜索后,我发现了一些建议:
vimgrep /<pattern>/ %
copen
Run Code Online (Sandbox Code Playgroud)
可以创建执行这两个命令的映射。模式应该是当前单词:可能是 vim 中的 cword 关键字?
我建议使用lvimgrep(这样你就可以使用quickfix来:make)
:nnoremap <F6> :lvimgrep /\M\<<C-R><C-W>\m\>/ **/*.[ch]pp **/Makefile | lopen<CR>
Run Code Online (Sandbox Code Playgroud)
另外,如果您只想在当前文件中查找:
:g/<pattern>/
Run Code Online (Sandbox Code Playgroud)
将在每个匹配行上调用“print”(默认命令)。
:v// " non-matching lines
:g//-1 " lines preceding the matching line
:g//-1,+1 " lines around the matching line
Run Code Online (Sandbox Code Playgroud)
ETC。
:global 更有用:
:g/foo/ join " join all lines containing foo
Run Code Online (Sandbox Code Playgroud)
ETC。
要求其实很简单。但要获取用户输入的模式,您需要一个函数。
function! FindAll()
call inputsave()
let p = input('Enter pattern:')
call inputrestore()
execute 'vimgrep "'.p.'" % |copen'
endfunction
Run Code Online (Sandbox Code Playgroud)
如果你想有一个映射,添加这一行:
nnoremap <F8> :call FindAll()<cr>
Run Code Online (Sandbox Code Playgroud)
但正如我在你的问题下评论的那样。%可能不适用于未命名的缓冲区。