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.
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:
If you just want to convert part of a document, say a highlighted visual block, you can just omit the percentage sign:
Bam! Your document is converted. Neat, huh?