tags:

views:

223

answers:

6
+2  Q: 

Find a file in Vim

Hey,

I'm new to VIM, just thought I'd give it a go so I'm still not 100% on what it is capable of.

Is there anyway to search a directory recursively for a file (using wildcards when needed)?

If not is there a plugin that can handle this?

Thanks in advance

A: 

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 like find or grep from vim by executing it like this

:!find ...
Nikolaus Gradwohl
A: 

You can use ! to run shell commands :

:! find . -name *.xml
Peter Tillemans
+3  A: 

vim as a builtin find command (:help find) but only open the first found file. However you can use this amazing plugin : FuzzyFinder which does everything you want and even more

mb14
+1  A: 

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:

See also this thread on SuperUser.

Paul Ruane
NERDTree is nice.
David Winslow
+1  A: 

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

:set path

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

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

the bit before the ** I copied from the initial :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)

StephenPaulger
If the file name is very long, this method isn't very comfortable, or does it do `my*.txt` also?
Shrikant Sharat
+3  A: 

You can use wildcards with the :edit command. 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 the wildcards before opening the file. See also the wildmode option for a way to browse through all possible extensions instead.

Another trick is to use

:r! find . -type f

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

David Winslow
+1 I never tried the wildcard in edit command. That works really well with the wildmode as well. So you can type :e **/Suite<tab> and all have the list of possible files
mb14
I, in turn, had not tried wildmode with wildcard expansion. I updated my answer with a mention.
David Winslow