Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a file (via recursive directory search) in Vim

Tags:

vim

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?

like image 592
mat-mcloughlin Avatar asked Aug 24 '10 08:08

mat-mcloughlin


People also ask

How do I search for a recursive file in Linux?

The grep command is used to search text or scans the given record for lines containing a match to the given strings or words. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.

What is a recursive file search?

Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.


2 Answers

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.

like image 94
David Winslow Avatar answered Oct 13 '22 06:10

David Winslow


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)

like image 20
Stephen Paulger Avatar answered Oct 13 '22 06:10

Stephen Paulger