How to modify filetype detection for filename extension?

I’m trying to change the filetype of a certain file extension by directly modifying /usr/share/nvim/runtime/lua/vim/filetype.lua. For example this is the diff:

@@ -1049,7 +1049,7 @@
   txi = 'texinfo',
   texinfo = 'texinfo',
   text = 'text',
-  tfvars = 'terraform-vars',
+  tfvars = 'terraform',
   thrift = 'thrift',
   tla = 'tla',
   tli = 'tli',

However, opening *.tfvars file still ended up with ‘terraform-vars’ filetype being set. Why doesn’t it work?

Many .lua files are compiled into the nvim binary, so modify the source file doesn’t work. You can use vim.filetype.add() to modify filetype detection:

:lua vim.filetype.add({ extension = { tfvars = 'terraform' } }) 
1 Like

Many thanks for this information