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.pyto the list of suffixes Vim tries when resolving filenames forgf,:find, and similar commands- When you press
gfonutils, Vim will tryutils, thenutils.js, thenutils.ts, thenutils.pyuntil 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
pathoption (: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