Associate filetype with file extension in Vim

Templates, syntax highlighting, smart auto-completion, and snippets, all rely on the detection of the filetype of the file being opened in Vim. Assuming that the filetype is set to ON in your ~.vimrc, you just need to specify the following one-liner to associate a file-extension with a file-type.

Suppose your file extension is myext and you wish to treat it as being of filetype html the just add the following line to your ~/.vimrc.


filetype on
autocmd BufRead,BufNewFile *.myext set filetype=html

It is now easy to verify that the filetype is being correctly identified. Open a file with the desired extension in Vim and in the normal (command) mode, issue the following command.


:set filetype?

It should show html.

That's it.