2

I am working with a new old code base where virtually all the interesting source files are three levels deep in the directory hierarchy. It's about 1.3M lines of code in more than 2000 .cpp files. The project Makefile is set up so that when a compiler error happens, only the file name part of the file in question is shown, rather than the path to that file.

What I'd like to do is be able to ask vim to open a file by name only, and have it look through the directory tree to find that name for me.

I already have a tags file set up so I can jump to identifiers without caring which directory or source file name they're in, but that doesn't help for actual file names themselves.

Is there a native vim way, or a plugin, that can help with this? My ideal solution would be something that would modify the :n command so that:

  • If a path is provided (even just ./), the behaviour is unchanged;
  • If only a file name is provided, and a file by that name doesn't exist in the current directory, vim would search through the whole tree under the current directory looking for a file by that name. If one is found, open that.

A solution that uses a different command than :n would be fine.

Greg Hewgill
  • 311
  • 1
  • 8
  • 2
    this should be more ore less easy with vimscript (look at findfile() http://vimhelp.appspot.com/eval.txt.html#findfile%28%29). If it really needs to overwrite the :n command this may help: http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev. However I would use something like this https://github.com/junegunn/fzf it is not exactly the solution to your :n command, but in my experience this changes (improves) your whole vim workflow and you don't need that stuff. But my experience is quite limited, so maybe have a look at it, but don't let me dictate your workflow =) – B.G. Oct 06 '17 at 05:49
  • @DoktorOSwaldo: Thanks, your hint led me to an easy built-in solution! – Greg Hewgill Oct 06 '17 at 07:01

2 Answers2

8

You can do this by setting the 'path' option to include **, and then using the :find command:

:set path=.,,**
:find filename.cpp

The section on File Searching in the documentation describes how the ** path wildcard works.

Greg Hewgill
  • 311
  • 1
  • 8
1

CtrlP should help you. Once you set it up, it is as simple as pressing Ctrl + P (or you can have custom mapping) and typing(or pasting) the name of file.

vikrant
  • 111
  • 1