3

:find and :edit both edit files and appear to need the relative or absolute path provided if the file isn't in the current directory. What is the extra part that :find brings? What is 'path'?

Help for :find:

:fin[d][!] [++opt] [+cmd] {file}
Find {file} in 'path' and then :edit it.
{not in Vi} {not available when the +file_in_path
feature was disabled at compile time}

Help for :edit:

:e[dit] [++opt] [+cmd] {file}
Edit {file}.
This fails when changes have been made to the current
buffer, unless 'hidden' is set or 'autowriteall' is
set and the file can be written.
Also see ++opt and +cmd.
{Vi: no ++opt}

4

The difference is that :find will actually search for a file by that name in the list of directories specified in 'path'.

The default 'path' contains . (which is the path of the current file), then /usr/include (on Unix platforms, like Linux and Mac OS) and then an "empty" entry, corresponding to the current directory.

Which means if you're editing src/main/main.c and you use :find args.c, Vim will first search for it in src/main/, which makes the default useful when you open many files in the same directory.

You notice your file includes <stdio.h> and you would like to open this header file, then :find stdio.h will find that in the system include directory. (An especially useful shortcut to this one is gf, which will :find the file name under the cursor, also using 'path'.)

Finally, if you use a path relative to the current directory, one that will work with :edit, it will also work with :find.


And the behavior of :find can be easily customized, by changing 'path' to a setting that's more appropriate for your filetype (programming language) and specific project, to make it really easy to navigate between related files and browse files in a runtime's standard library.

To give you some ideas of how powerful 'path' can be, consider these settings:

:set path=.git/..;

This will traverse up the tree, trying to find a .git directory, then use the parent of that directory. In other words, this simple expression will look for the root of your git checkout and allow you to use :find to access paths relative to this directory.

Another powerful example is:

:set path=**

Which will traverse down the tree and allow you to open a file under any subdirectory in the tree. (Note that you should avoid this setting, it's preferable to have 'path' set in a semantically appropriate way for your language and/or project, which allows you to quickly access included files following the same path search as the language.)

You might want to consider enhancing this experience with plug-ins, in which case some recommendations are:

  • apathy.vim: Set 'path' and friends for languages such as C, Python, JavaScript and Go.
  • projectionist.vim: Understands your project's paths and sets 'path' appropriately.
  • Other language-specific plug-ins, such as vim-ruby for the Ruby language.
| improve this answer | |
  • 1
    Great answer. Thank you. I missed the fact that 'path' in the manual was a link to where it's described (new users: hit CTRL-] while on 'path'). :find is a powerful feature I wish I started using years ago. – htoip Mar 13 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.