vimtricks.wiki Concise Vim tricks, one at a time.

How do I open a file found in Vim's path setting in a new split without typing the full path?

Answer

:sfind {file}

Explanation

:sfind (split-find) searches Vim's path setting for a file matching the given name and opens it in a new horizontal split, all in one command. It combines the file-discovery power of :find with :split, so you do not need to know where the file lives as long as its directory is in path.

How it works

  • :sfind {name} — searches path for {name}, opens in a horizontal split
  • :vertical sfind {name} — same, but opens in a vertical split
  • Tab completion works: type :sfind Par<Tab> to complete file names found in path
  • Wildcards are supported: :sfind *.h opens the first matching header in a split

Example

With set path+=** (to search recursively from the current directory):

" Open utils.py in a split without navigating to it first
:sfind utils.py

" Open it vertically with tab completion
:vertical sfind util<Tab>

Compare the family of find commands:

:find utils.py        " open in current window
:sfind utils.py       " open in horizontal split
:vertical sfind utils.py  " open in vertical split
:tabfind utils.py     " open in a new tab

Tips

  • Set set path+=** in your vimrc to search recursively from the project root
  • set wildmenu makes tab-completion of file names interactive
  • set suffixesadd=.py,.js,.ts lets :sfind find files without typing the extension
  • :find and :sfind respect set wildignore to skip unwanted files like node_modules

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?