使用 SpaceVim 在项目中的所有目录(和子目录)的所有文件中搜索文本

Search text inside all files in all directories (and subdirectories) in project with SpaceVim

所以我刚开始使用 Neovim/Spacevim,它太棒了!

我还在适应一切,因为我以前从未使用过 Vim 或类似的东西。

我的问题围绕着在当前打开的项目的所有文件中搜索特定文本。

我正在使用 nerdtree 文件管理器,我想知道如何在项目中的所有文件中搜索特定字符串。就像如果我想在当前打开的 folder/directory 中搜索 function thisExactFunction(),我该怎么做?主要目标是获得包含此搜索字符串的所有文件的列表。

我安装了 fzf(以及 ripgrep),但似乎无法在所有文件中搜索特定文本。我只能搜索文件本身,或者无法产生我需要的某些其他搜索。

谁能指出我正确的方向...?谢谢!

查看 Fzf 提供的 Ggrep 命令 - 请参阅此 series of vim screencasts 了解如何使用 vim 的内置功能(由 :[=28= 填充的快速修复列表]grep) 使用其他 grepping 工具实现同样的效果。

自定义函数

我的 .vimrc 中有一个函数使用 ag 银 searcher 在所有范围内搜索 目录(和任何子目录)中的文件。所以如果你安装 ag,这个 应该工作:

" Ag: Start ag in the specified directory e.g. :Ag ~/foo
function! s:ag_in(bang, ...)
    if !isdirectory(a:1)
        throw 'not a valid directory: ' .. a:1
    endif
    " Press `?' to enable preview window.
    call fzf#vim#ag(join(a:000[1:], ' '),
                \ fzf#vim#with_preview({'dir': a:1}, 'right:50%', '?'), a:bang)
endfunction

" Ag call a modified version of Ag where first arg is directory to search
command! -bang -nargs=+ -complete=dir Ag call s:ag_in(<bang>0, <f-args>)

奖金

有时很难在vim的帮助中找到东西,所以我也有一个功能 使用上面的那个以交互方式搜索帮助文档。这可能很好 磨练你想要的话题。对这个函数使用 :H(相对于 经典 :h)

function! Help_AG()
    let orig_file = expand(@%)
    let v1 = v:version[0]
    let v2 = v:version[2]
    " search in the help docs with ag-silver-search and fzf and open file
    execute "normal! :Ag /usr/share/vim/vim".v1.v2."/doc/\<CR>"
    " if we opened a help doc
    if orig_file != expand(@%)
        set nomodifiable
        " for some reason not all the tags work unless I open the 'real' help
        " so get whichever help was found and open it through Ag
        let help_doc=expand("%:t")
        " open and close that help doc - now the tags will work
        execute "normal! :tab :help " help_doc "\<CR>:q\<CR>"
    endif
endfunction

" get some help
command! H :call Help_AG()