Markdown syntax highlighting and conversion in vim

Recently I’ve been playing around with vim as an editor. Since I do most of my writing in a text editor, one of the first functions I found myself looking for was a way to do basic markdown syntax highlighting and formatting. Here’s how I set up my vim environment to do both of that.

Syntax highlighting

PlasticBoy got me most of the way there when it came to syntax highlighting. All I had to do was figure out how it fit into my .vim directory structure, which is a clone of Martin Greffel’s excellent setup. This turned out to be pretty straightforward

  • Save the mkd.vim file to ~/.vim/syntax/mkd.vim
  • Put the following in ~/.vim/ftplugin/mkd.vim:
    " markdown filetype file
    if exists("did\_load\_filetypes")
     finish
    endif
    augroup markdown
     au! BufRead,BufNewFile *.mkd   setfiletype mkd
    augroup END
  • For file type auto detection (i.e. if you want syntax highlighting automatically loaded for files with extensions like .markdown and .mdown), add this to ~/.vim/ftdetect/markdown.vim:
    autocmd BufNewFile,BufReadPost *.mkd,*.markdown,*.mdown set filetype=mkd

Converting a document or selection from Markdown to HTML

Converting markdown to html from inside of vim is nice and easy. In fact, you don’t even need the syntax files installed. You do need the markdown script installed somewhere (say, /usr/local/markdown).

We can take advantage of vim’s command line filtering here, and simply run the markdown command on all or part of the document, right from within vim. To convert the entire document, use the percent sign:

:%!/path/to/markdown --html4tags

If you just want to convert part of a document, say a highlighted visual block, you can just omit the percentage sign:

:!/path/to/markdown --html4tags

Bam! Your document is converted. Neat, huh?

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

You can follow any discussion on this article with the RSS feed for this post.

About this article

Green Galoshes is a weblog written by Justin D. Henry. This entry was published on or around December 22, 2009.

Categories & Tags

This article is filed under reference, tools. It is further described as , , , , , .