neovimfzffuzzyfinderspacevim

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


So I just started using Neovim/Spacevim, and it's so awesome!

I am still getting used to everything, since I have never used Vim or anything like that before.

My question revolves around searching for particular text in all the files of the currently open project.

I am using the nerdtree file manager, and I was wondering how I might search through all the files in the project for a specific string. Like if I wanted to search function thisExactFunction() throughout the currently open folder/directory, how would I go about doing that? The main goal is to have a list of all the files that contain this search string.

I have fzf installed (as well as ripgrep), but seem to be having trouble in searching for specific text inside of all files. I can only search for files themselves, or some other search that does not yield what I need.

Can anyone point me in the right direction...? Thanks!


Solution

  • Check out the Ggrep command that Fzf supplies - see this series of vim screencasts to see how to use vim's in built features (quickfix list populated by :vimgrep) to achieve the same using other grepping tools.

    Custom functions

    I have a function in my .vimrc that uses ag silver searcher to search within all the files in a directory (and any subdirectories). So if you install ag, this should work:

    " 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>)
    

    Bonus

    Sometimes it's hard to find things in vim's help, so I also have a function that uses the one above to interactively search the help docs. This can be nice to hone in on the topic you want. Use :H for this function (as opposed to the classic :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()