Find a file (via recursive directory search) in Vim

Viewed 65973

Is there any way to search a directory recursively for a file (using wildcards when needed) in Vim? If not natively, is there a plugin that can handle this?

15 Answers

You don't need a plugin only for this function, below code snippet is enough.

function! FindFiles()
    call inputsave()
    let l:dir = input("Find file in: ", expand("%:p:h"), "dir")
    call inputrestore()
    if l:dir != ""
        call inputsave()
        let l:file = input("File name: ")
        call inputrestore()
        let l:nf = 'find '.l:dir.' -type f -iname '.l:file.' -exec grep -nH -m 1 ".*" {} \;'
        lexpr system(l:nf)
    endif
endfunction
nnoremap <silent> <leader>fo :call FindFiles()<CR>

ag tool and corresponding Ag vim plugin solves this problem perfectly:

To find a file using some pattern use:

AgFile! pattern

It will open quickfix window with results where you can choose.

You can add vim keybinding to call this command using selected word as a pattern.

nnoremap <silent> <C-h> :AgFile! '<C-R><C-W>'<CR>
vnoremap <silent> <C-h> y :AgFile! '<C-R>"'<CR>
Related