48

How To Add A File Extension To vim Syntax Highlighting


Posted by Artem Russakovskii on April 2nd, 2008 in Databases, Linux, Programming
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?

image

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:

image

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:

image

After

1
2
3
syntax on
filetype on
au BufNewFile,BufRead *.dump set filetype=sql

everything is fine:

image

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.



Share

48 Responses to “How To Add A File Extension To vim Syntax Highlighting”

    48 Comments:
  1. Thaya says:

    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.

  2. Jimmy Janks says:

    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!

  3. It was very helpful. Thanks!

  4. Srikanth says:

    Thanks for the tip, this helped me today :)

  5. roger says:

    Thanks a lot !
    I was looking for this for a long time !

    Bye,
    Roger

  6. Shushan says:

    You made my day. Thanks!

  7. kunthar says:

    In case this recipe doesn't works for anyone, just use this;
    :syntax on
    :set syn=php or whatever extension you want.

  8. Merijn says:

    Thanks for the tip!

  9. Amy says:

    You are awesome. Thanks for this!

  10. Phani T says:

    Thanks a lot dude.
    i was looking for this tip from a long time.
    Its working just fine.

  11. Vishal Kotcherlakota says:

    Running great on Vim on Slackware! Thanks for putting out this tip. It saved me a lot of digging through Vim's documentation. =)

  12. chrish says:

    Awesome! Real time saver. Was preparing to RTFM :)

  13. neurox says:

    Thanks – I needed this badly for Magento's .phtml files (which are actually PHP). Awesome! :)

  14. Joel says:

    Thanks google, and thanks to you for the great tip. works like a champ.

  15. Yo says:

    It may have been a while but it's still useful. Thanks!

  16. John says:

    Just what I was looking for.

    Thanks dude!

  17. Carlos says:

    Thanks! It's exactly what I needed.

  18. Jake says:

    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

  19. Alexander says:

    Thx for the solution. I was needed to edit *.class files as php-scripts and you did a great help to me =)

  20. Mohit says:

    Thanks for the tip. Just to add to this, the filetype should be all in small letters. I wanted to set the filetype to "Specman" (as it appears in filetypes list in the gvim menu), it didn't work untill I changed the filetype to "specman".

  21. Justin says:

    Good one! You fixed my fugly phtml files.

  22. Jie says:

    so cool artem! i searched on google and it gave me an answer. not that it did not happen before. but this time, the answer is from someone i know~

  23. Matt Darby says:

    Still helping people! Thanks ;)

  24. Jorge Medina says:

    Thanks

    I already knew how to use :setf, this is much better.

    Have a good day.

  25. Harry Stamper says:

    Thank you, man.
    I hope you get to be millionaire.

  26. Evelyn says:

    I had to edit a .ofx file today. Following your example i added this to my .gvimrc file

    augroup OFX
    au! BufNewFile,BufRead *ofx set filetype=xml
    augroup end

    Worked great!

  27. John Murray says:

    This was very helpful! Thank you!

  28. martin says:

    thanks for the hint.
    i love vim. it is a pain in the ass to get started but once you are in to it, you will never choose another text editor!
    best text editor for hackers.

    see ya folks

  29. k00pa says:

    Thanks! Just what I needed :P

  30. Merker says:

    Perfect ! thanks

  31. Snajperka89 says:

    Years are passing and your tip is immortal :) Thanks!

  32. AnotherHappyUser says:

    Another Happy user for this tip.

    Thanks :)

  33. kaewgb says:

    Thanks a lot! Really useful :)

  34. Jason says:

    Thank you. I've been staring at a lot of black and white, and you just brought color to my world. :)

  35. Scott says:

    Thanks for the tip. Now my .less files look like proper CSS.

  36. Brooke says:

    Thanks! I was able to get Vim (on Mac Terminal.app) to recognize as Markdown files wth the extenson *.md. (Previously, it only recognized as Markdown files with extenstion *.markdown.)

    filetype on
    au BufNewFile,BufRead *.md set filetype=markdown

    Thanks again!

  37. Adam Pippin says:

    Only issue I ran into was that I did not add "syntax on", as it was already in my .vimrc. Unfortunately, it was added as follows:

    if has('syntax') && (&t_Co > 2)
    syntax on
    endif

    The file type did not have a syntax (until I later defined one), and thus it was never enabled. I prefer my "syntax on" only in those conditions, so I forced it on for the extension in question by entering as:

    au BufNewFile,BufRead *.file syntax on
    au BufNewFile,BufRead *.file set filetype=html

    Cheers!

  38. ani says:

    thanks for the tip.
    works like a charm.

  39. sckyo says:

    this is cool thanks for the tip

  40. Andreas says:

    I think this is one of thе most significant infο
    foг me. And i'm glad reading your article. But want to remark on some general things, The web site style is ideal, the articles is really nice : D. Good job, cheers

    Also visit my homepage … Andreas

  41. Yaniv says:

    Thank you. it helped me.

    if you need verilog, just put

    au BufNewFile,BufRead *.v_et set filetype=verilog

    on the GUI it is "Verilog HDL" which will not work on the command line

  42. Suresh says:

    Many Thanks.

    This tip helped me a lot.

  43. Sandra says:

    Helpful info. Lucky me I discovered your website
    by accident, and I'm shocked why this accident did not came about earlier! I bookmarked it.

    My web blog: click here

  44. Sherrill says:

    I'm really enjoying the design and layout of your website. It's a very easy on the
    eyes which makes it much more enjoyable for me to come
    here and visit more often. Did you hire out a developer to create your theme?

    Great work!

    Here is my website; Sherrill

  45. Etta says:

    A person necessarily lend a hand to make critically articles I'd state. That is the first time I frequented your web page and thus far? I amazed with the research you made to make this actual put up incredible. Great job!

    Look into my web site … appliance repair tampa free estimates

Leave a Reply