I'm trying to set up my .vimrc
so gf
will automatically work on paths that are missing file extensions, by trying to open files with the same extension as the current FileType.
In other words, I want something like:
autocmd FileType <filetype> setl suffixesadd+=<exts>
where <exts>
is a list of all file extensions associated with the current <filetype>
.
For example, my filetype.vim
defines the FileType "javascript" as files with names *.js
,*.javascript
,*.es
,*.jsx
, and *.json
, so whenever I am editing a javascript buffer, if the cursor is on the path ./index
, running gf
should try to open ./index.js
. If that file doesn't exist then it should try ./index.javascript
, and so on. If the filetype is python, it should try ./index.py
, ./index.pyw
, etc.
I'm pretty sure the autocmd above should produce the intended behavior if I just run it for every FileType, but I'm not sure how to do that.
Those default extensions are not stored anywhere in a useful format so you will need to build your own list and loop through it to run your autocommand.
Something like:
augroup suffixes
autocmd!
let associations = [
\["javascript", ".js,.javascript,.es,.esx,.json"],
\["python", ".py,.pyw"]
\]
for ft in associations
execute "autocmd FileType " . ft[0] . " setlocal suffixesadd=" . ft[1]
endfor
augroup END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With