| Share |
Updated: July 8th, 2009
Today I was asked a question about defining custom extensions for vim syntax highlighting such that, for example, vim would know that example.lmx is actually of type xml and apply xml syntax highlighting to it. I know vim already automatically does it not just based on extension but by looking for certain strings inside the text, like <?xml but what if my file doesn't have such strings?
After digging around I found the solution. Add the following to ~/.vimrc (the vim configuration file):
1 2 3 | syntax on filetype on au BufNewFile,BufRead *.lmx set filetype=xml |
After applying it, my .lmx file is highlighted:
Same principle works, for instance, for mysql dumps that I have to do from time to time. If they don't have a .sql extension, you'll get something like:
After
1 2 3 | syntax on filetype on au BufNewFile,BufRead *.dump set filetype=sql |
everything is fine:
But why and how does it work, you ask?
| :help au | :au[tocmd] [group] {event} {pat} [nested] {cmd} Add {cmd} to the list of commands that Vim will execute automatically on {event} for a file matching {pat}. |
| :help BufNewFile | When starting to edit a file that doesn't exist. |
| :help BufRead | When starting to edit a new buffer, after reading the file into the buffer. |
| :help filetype | will actually tell this whole story in part B. |
And that's how you do it, folks.
Artem Russakovskii is a San Francisco programmer, blogger, and future millionaire (that last part is in the works). Follow Artem on Twitter (@ArtemR) or subscribe to the RSS feed.
In the meantime, if you found this article useful, feel free to buy me a cup of coffee below.


beer planet is a blog about technology, programming, computers, and geek life. It is run by Artem Russakovskii - a local San Francisco geek who currently works at
I used to hate VIM when I first started programming. Now I am powerless without it. It's amazing how feature-rich the text editor is. I still learn new VIM tricks 3-4 times a month.
This is the best ever, thanks for posting this!
I would only add that you can spare yourself the "syntax on" and "filetype on" lines if these are already your defaults and just add the new to-be-highlighted extensions with the "au" line.
Belissima!
It was very helpful. Thanks!
Thanks for the tip, this helped me today
Thanks a lot !
I was looking for this for a long time !
Bye,
Roger
You made my day. Thanks!
In case this recipe doesn't works for anyone, just use this;
:syntax on
:set syn=php or whatever extension you want.
Thanks for the tip!
You are awesome. Thanks for this!
Thanks a lot dude.
i was looking for this tip from a long time.
Its working just fine.
Running great on Vim on Slackware! Thanks for putting out this tip. It saved me a lot of digging through Vim's documentation. =)
Awesome! Real time saver. Was preparing to RTFM
Thanks – I needed this badly for Magento's .phtml files (which are actually PHP). Awesome!
Thanks.
Thanks google, and thanks to you for the great tip. works like a champ.
It may have been a while but it's still useful. Thanks!
Just what I was looking for.
Thanks dude!
Thanks! It's exactly what I needed.
Hey I am trying to do this but it is not quite working as expected. It will highlight that file type BUT it also highlights other file types using that highlighting. (Bad).
My problem is documented here if anyone has any input that would be much appreciated. (You don't need a username or password to reply).
http://superuser.com/questions/113455/vim-highlighting-for-specific-file-types
Jake
Looks like you found the solution. SuperUser, StackOverflow, etc are great, aren't they?