pixelearth
pixelearth

Reputation: 14620

Find files in vim: how to do a partial filename match with Explore **/*

I've recently discovered the following to find files in Vim.

 :Explore **/[pattern]

Finding files is pretty important to me, and I can't believe I've done without it for 8 years. I can relate to what this gentleman has said: http://vim.wikia.com/wiki/Find_files_in_subdirectories

I see people searching for files in TextMate and have to hang my head in shame :(

So using the above I'm able to search for files, "in theory", but the output baffles me and often has files that do not match. I'll use a Rails example.

For example doing this:

Explore **/envir*

Yields this, which a bunch of extra weird files:

../
deploy/
environments/
initializers/
locales/
.DS_Store
application.rb
authorization_rules.rb
boot.rb
config.yml
database.yml
deploy.rb
development.sphinx.conf
environment.rb
production.sphinx.conf
routes.rb
sphinx.yml
staging.sphinx.conf
test.sphinx.conf

That is at least usable, I can scroll down to environment.rb and open it.

But say I want to find a list of all helpers. I would think that this:

Explore **/*help*

Would work, but no files are found.

Can someone illuminate me?

Upvotes: 2

Views: 3332

Answers (3)

lomilomi26
lomilomi26

Reputation: 33

If all you want is simple :find type functionality but with partial file name matching with tab auto-completion, the find-complete plugin is good.

Upvotes: 0

romainl
romainl

Reputation: 196556

No, :Ex[plore] **/foo* sucks.

The above mentioned CtrlP is a beauty (that I use extensively despite a few limitations) that allows you to find files in your project and other niceties. FuzzyFinder and Command-T are worthy alternatives, LustyExplorer as well and there are obviously many others.

But Vim is quite capable by himself. The wildmenu option is key, here. Just add these two lines to your ~/.vimrc:

set wildmenu           " see :h 'wildmenu'
set wildmode=list:full " see :h 'wildmode' for all the possible values

With the setting above, the following command shows you an accurate list of matches that you can navigate easily and relatively intuitively.

:e **/foo*<Tab>        " see :h starstar

If the "working directory" is set to your project directory, that command is sure to give you good results. It's not very sexy (I like it, though) but it's very effective and dependable. Of course, this method can be used with :sp, :vs or :tabe.

The :find command is even better and probably more suited to yor needs: it looks into the directories defined in the path option. :set path=/path/to/project at the beginning of your session and you can open any file in your project with:

:find foo<tab>         " see :h :find

Neat.

Another possibility is to use "args". You add all the files matching some pattern to your "argument list" and use buffer navigation commands:

:argadd ./**/*.js      " see :h arglist
:sb foo<tab>

But it can be a little messy.

Overall, there are many elegant ways to deal with file navigation in Vim (and I didn't even mention tags-based navigation!). Plugins may provide a nice unified UI to a number of navigation needs but they are not required for finding files efficiently. At all.

It is interesting to note that both TextMate's and Sublime Text's implementations of fuzzy search are actually limited to the current "project" which is not the case for those Vim plugins or for Vim's buit-in file handling.

Upvotes: 3

Peter Stuifzand
Peter Stuifzand

Reputation: 5104

Have you look at the Ctrl-P.vim plugin? https://github.com/kien/ctrlp.vim

This plugins finds files in a similar way to what you show.

Upvotes: 3

Related Questions