How do I make gf find files when import paths omit the file extension?
Answer
:set suffixesadd+=.js,.ts,.jsx,.tsx
Explanation
The gf (go to file) command opens the file under the cursor, but it fails when the path lacks an extension — common in JavaScript/TypeScript imports like import Foo from './components/Foo'. By setting suffixesadd, Vim will automatically try appending each listed suffix when looking for the file, making gf work seamlessly with extension-less imports.
How it works
:set suffixesadd+=.js,.ts,.jsx,.tsx— tells Vim to try these extensions whengfcannot find the exact filename- When you press
gfon./components/Foo, Vim triesFoo, thenFoo.js, thenFoo.ts,Foo.jsx,Foo.tsxin order - The first match wins and opens in the current window
This works with gF (go to file + line number), <C-w>f (open in split), and <C-w>gf (open in tab) as well.
Example
Given this import statement with the cursor on the path:
import Header from './components/Header'
Without suffixesadd, pressing gf would fail with E447: Can't find file "./components/Header".
After :set suffixesadd+=.js,.ts,.jsx,.tsx, gf successfully opens ./components/Header.tsx.
Tips
- Set this per filetype in your vimrc:
autocmd FileType javascript,typescript setlocal suffixesadd+=.js,.ts,.jsx,.tsx - For Python projects, use
suffixesadd+=.py - Combine with
:set path+=src/**to resolve project-relative imports - Check the current value with
:set suffixesadd?