Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #1146 - Project browsing using find

Created: February 22, 2006 17:03 Complexity: basic Author: gautamvsg Version: 5.7 Karma: 12/6 Imported from: Tip#1146

Tip is for loading project files that you know the names for but do not necessarily remember their paths.

Usually when you are working on a project and you want to load a particular file that is somewhere deep in the directory hierarchy, you have to either find the file on the command line and then load it into vim, or use vim tab completion to navigate and load the file. You can use vim's "find" feature instead.

Make the following entry in you .vimrc:

set path=$PWD/** 

This will set your path variable to current directory (from which you launched vim) and to all directories under current directory recursively.

Now all you have to do is to open vim/gvim from the base directory of your project. Then if there is a file that you want to open but dont remember the path for it, do a

:find <full-file-name-including-extension> 

and it will open the file that you want.


Caveats:

  • If you have multiple files with the same name, then you are in trouble. Find will not list them out (like cscope/ctags) before it opens the file. You may end up making edits in the wrong file.
  • The find does not take any wildcards. So you cannot afford to make spelling mistakes in the file names (esp. java names)


In spite of the caveats, finding in this manner is far faster than navigating through directories to look for files (esp. if you are a touch typist.)


Variations:

  • You can change your path variable anytime to change the find path.
  • You can use :sfind instead of :find to open it in a split window.
  • Coupled with cscope (see :help cscope) you editing code across files becomes very convenient.


What would be a great feature in vim would be something similar to the "Fast Open Class" feature in Netbeans. Type Alt-shift-O in Netbeans and it will give you a dialog where you can type the file that you are looking for. While typing, it incrementally displays the classes that match your search. Press enter when you have found what you want. (No need of using the mouse at all..)

Comments

Works under linux on a project with ~10,000 files. Searches take about 2 seconds....nice tip.


jasonbeck--AT--san.rr.com , February 22, 2006 20:28


Since v58 of netrw, which is currently up to v78, there's

:Explore **/pattern 

which brings up directory listings based on files matching the pattern. ex.

:Explore **/*.txt 

The cursor is left on the matching file so it can be brought up easily. To skip from one matching file to the next:

:Nexplore (or, if you have a mouse enabled: <s-down>) 
:Pexplore (or, if you have a mouse enabled: <s-up>) 

(you'll need vim 7.0aa or later to use this)


NdrOchip--AT--ScampbellPfamily.AbizM - NOSPAM , February 23, 2006 9:23


See also script#949.

lpenz , February 23, 2006 9:30


There was a previous tip in the same thematics. See VimTip1075 (the beauty of gf).

Regarding the fact several files may match, you can have a look at the plugin SearchInRuntime I'm maintaining.
The commands defined don't have any problem with wildcards, they even support auto-completion.
It does not provide a command to edit in the current buffer, but two commands to edit in a split (horizontally or vertically) window. If the file is already opened in a window, we jump to that window.
The caveat is that vim7 is required.

--Luc Hermitte, July 7, 2006 9:39


On windows use, set path=.\**


Advertisement