Find file (via search in recursive directory) in Vim

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

+47
vim
Aug 24 '10 at 8:32
source share
13 answers

You can use wildcards with the command :edit . So,

 :e **/test/Suite.java 

will open test / Suite.java no matter where it is in the current directory hierarchy. This works with tab completion, so you can use [tab] to expand wildcards before opening the file. See Also wildmode parameter to see all possible extensions.

Another trick is using

 :r! find . -type f 

load the list of all files in the current directory into the buffer. Then you can use all the usual vim text processing tools to navigate / sort / crop the list and CTRL+W gf to open the file under the cursor in a new panel.

+68
Aug 24 '10 at 21:01
source share

There is a find command. If you add ** to your path, you can search recursively.

 :set path 

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

 :set path=.,/usr/include,,** 

bit before ** I copied from the start :set path

then you can simply enter

 :find myfile.txt 

and it opens in a magical way!

If you add the set command to your .vimrc, it will be sure that you can do a recursive search in the future. It doesn't seem to be looking for point directories (e.g. .ssh)

+28
Aug 24 '10 at 9:02
source share

I would recommend ctrlp.vim . This is a very good plugin that is ideal for working inside large projects. It has a search by file name or full path, regular expression search, automatic detection of the project root (the one that contains the .git | hg | svn | bzr | _darcs folder), personalized file name exceptions and much more.

Just press <cp> and it will open a very intuitive panel where you can search for what you want:

enter image description here

You can immediately select and open multiple files. It also accepts additional arbitrary commands, such as going to a specific line, the appearance of a line, or any other Vim command.

Repo: https://github.com/kien/ctrlp.vim

+12
Aug 07 '15 at 12:27
source share

vim as a built-in find command (: help find), but just open the first file found However, you can use this amazing plugin: FuzzyFinder , which does everything you need and more.

+5
Aug 24 '10 at 8:55
source share

Command-T allows you to quickly find a file by simply typing a few letters. You can also open the file in a new tab, but it needs a vim compiled with ruby ​​support.

+3
Feb 27 '13 at 20:49
source share

vim has bild in commands named grep, lgrep, vimgrep or lvimgrep that can do this

here is a tutorial on how to use them http://vim.wikia.com/wiki/Find_in_files_within_Vim#Recursive_Search

you can also use an external command, for example find or grep, from vim by executing it as

 :!find ... 
+1
Aug 24 2018-10-10T00:
source share

You can use! to run shell commands:

 :! find . -name *.xml 
+1
Aug 24 2018-10-10T00:
source share

You can view the file system with :ex . but I don’t know how to search recursively (I am new to Vim - I only use it for ten years).

There are several popular plugins for file browsers:

See also this thread on SuperUser .

+1
Aug 24 '10 at 9:00
source share

View as Quickfix as

Using:

 Find my.regex 

Result:

  • new tab opens
  • each line contains a relative path that corresponds to grep -E regex
  • hit:
    • <enter> or <Cw>gf to open the file in the current line in a new tab
    • gf to open the file in the current tab and lose the list of files

Find all files:

 Find 

Alternative methods:

the code:

 function! Find(cmd) let l:files = system(a:cmd) if (l:files =~ '^\s*$') echomsg 'No matching files.' return endif tabedit set filetype=filelist set buftype=nofile " TODO cannot open two such file lists with this. How to get a nice tab label then? " http://superuser.com/questions/715928/vim-change-label-for-specific-tab "file [filelist] put =l:files normal ggdd nnoremap <buffer> <Enter> <CW>gf execute 'autocmd BufEnter <buffer> lcd ' . getcwd() endfunction command! -nargs=1 Find call Find("find . -iname '*'" . shellescape('<args>') . "'*'") command! -nargs=1 Gfind call Find('git ls-files | grep -E ' . shellescape('<args>')) command! -nargs=1 Gtfind call Find('git rev-parse --show-toplevel && git ls-files | grep -E ' . shellescape('<args>')) command! -nargs=1 Locate call Find('locate ' . shellescape('<args>')) 
+1
Mar 23 '16 at 23:37
source share

I am surprised that no one mentioned Unite.vim .

A file search (fuzzy one way or another) is just the tip of the iceberg of what it can do for the developer. It supports ag , git support and many other programs / utilities / vim plugins. The learning curve may be a little steep, but I can't imagine my life without it. The user base is large and bugs are fixed immediately.

+1
Apr 6 '16 at 13:44
source share

Depending on your situation (that is, if the following command finds only one file), perhaps use a command, for example:

 :e `locate SomeUniqueFileName.java` 

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

 locate SomeUniqueFileName.java 

Note that the magic here is the reverse steps around the command, which convert the output from the shell command into text that can be used in the Vim command.

+1
May 31 '17 at 17:26
source share

You can find files recursively in your "path" with this plugin . It also supports tab filling for file name.

0
Feb 06 '13 at 8:32
source share

You do not need a plugin just for this function, below the code snippet.

 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
Sep 22 '17 at 6:08
source share



All Articles