Saturday, October 16, 2010

vim custom syntax

I've been known to write blog posts. I tend to draft them in a text file, just because it's easier to edit in vim than in a text box and I get an automatic backup. The format is sorta HTML, but not quite: I don't have to bother with <p> tags, etc. To avoid confusion, I give them a file extension of .blog, but I want vim to highlight them like .html files.

Getting vim to do this is actually pretty straightforward, but figuring out how was a pain. The info is scattered across a bunch of help files and cluttered with references to old versions and non-recommended options. So I'm pulling it together here.

First, create a ~/.vim/syntax/blog.vim file containing:
runtime! syntax/xhtml.vim

This says to invoke the xhtml syntax file. (You could do plain html instead.)

Then you need to tell vim to use the blog.vim syntax when you're editing a .blog file. To do this, create a ~/.vim/ftdetect/blog.vim file containing:
au BufRead,BufNewFile *.blog        set filetype=blog

That's it.

No comments:

Post a Comment