Find a file (via recursive directory search) in Vim

后端 未结 15 1021
感情败类
感情败类 2020-12-22 18:42

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条回答
  • 2020-12-22 19:05

    Command-T lets you find a file very fast just by typing some letters. You can also open the file in a new tab, but it need vim compiled with ruby support.

    0 讨论(0)
  • 2020-12-22 19:05

    Depending on your situation (that is, assuming the following command will find just a single file), perhaps use a command like:

    :e `locate SomeUniqueFileName.java`
    

    This will cause Vim to open, in the current tab (the e command) a file that is the result of running (in this example),

    locate SomeUniqueFileName.java
    

    Note that the magic here is the backticks around the command, which will convert the output from the shell command into text usable in the Vim command.

    0 讨论(0)
  • 2020-12-22 19:07

    You can browse the file system with :ex ., but I do not know how to search recursively (I am a Vim novice — I have been using it only ten years).

    There are a few popular file browsers plug-ins:

    • NERD tree
    • Lusty explorer
    • vtreexplorer

    See also this thread on SuperUser.

    0 讨论(0)
  • 2020-12-22 19:07

    You can find files recursively in your "path" with this plugin. It supports tab completion for the filename as well.

    0 讨论(0)
  • 2020-12-22 19:09

    There is a find command. If you add ** (see :help starstar) to your 'path' then you can search recursively:

    :set path
    

    will show you your current path, add ** by doing something like

    :set path+=**
    

    then you can just type

    :find myfile.txt
    

    and it opens magically!

    If you add the set command to your .vimrc it'll make sure you can do recursive search in future. It doesn't seem to search dot directories (.ssh for example)

    0 讨论(0)
  • 2020-12-22 19:10

    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>
    
    0 讨论(0)
提交回复
热议问题