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

How do I make gf find files even when the import path omits the file extension?

Answer

:set suffixesadd+=.js,.ts,.py

Explanation

In many programming languages, import statements reference modules without file extensions (e.g., import utils instead of import utils.py). By default, Vim's gf command only finds the file if the exact filename exists. Setting suffixesadd tells Vim to try appending each listed extension when looking for files, making gf work seamlessly with extensionless imports.

How it works

  • :set suffixesadd+=.js,.ts,.py — appends .js, .ts, and .py to the list of suffixes Vim tries when resolving filenames for gf, :find, and similar commands
  • When you press gf on utils, Vim will try utils, then utils.js, then utils.ts, then utils.py until it finds a match
  • The += syntax appends to the existing list rather than replacing it

Example

Suppose your Python file has:

from helpers import validate

With suffixesadd configured:

:set suffixesadd+=.py

Place your cursor on validate and press gf. Vim will find and open validate.py even though the import didn't include the extension.

Tips

  • Set this per filetype in your vimrc using autocommands: autocmd FileType python setlocal suffixesadd+=.py
  • Combine with the path option (:set path+=src/**) to search subdirectories too
  • Use :set suffixesadd? to see the current value
  • Works with <C-w>f (open in split) and <C-w>gf (open in tab) as well

Next

How do I return to normal mode from absolutely any mode in Vim?