3

When open a file with :e, it have to be tell the exact path of the file, and even the temporary file will be listed, is there anyway to do

  1. Open a file with only name, and let it automatically search under :pwd
  2. Ignore certain file types, e.g. file~
1

1 Answer 1

3

You can use the :find command to locate a file by name and open it for editing.

:find will search for the file in the directories listed in 'path', which by default includes the current directory. You can add more directories to 'path' that you want to look up, and you can use wildcards, including ** to traverse multiple subdirectories.

In particular, to find files recursively under the current tree, you can use:

:set path+=**

NOTE: This can be very slow in trees with many subdirectories, so be careful with this setting. Prefer to specify narrower subsets of the tree, where to locate files you're searching for.

You can also take a look at :help file-searching for more details on the kinds of expressions available to use in 'path'.

Second, you can specify extensions to add to the file names, using the 'suffixesadd' setting. For example, if you're working on a project using Python, HTML and JavaScript, you can use something like:

:set suffixesadd=.py,.html,.css,.js

Having both 'path' and 'suffixesadd' set, you can use a command such as :find ui to open file static/javascript/frontend/ui.js. Pretty neat, huh? Or :find model to open src/db/model.py.

Note you always need to specify the full name of the file (except for the path and the extension.) If you wanted something that would handle incomplete filenames only, then consider plug-ins such as ctrlp.vim or fzf.vim to extend Vim with support for fuzzy finders.

You also asked about ignoring backup files. When you're completing filenames (for example, using Tab after a :e), you can ignore files matching certain patterns by adding them to the 'wildignore' setting.

For example:

:set wildignore=*~,*.bak

This will skip backup files when completing filenames on a :e command line.

1
  • 2
    path is a pretty useful option; ** may not be right for everyone (see the readme to tpope’s apathy, for example).
    – D. Ben Knoble
    Aug 3, 2019 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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