141
votes

I'm using Vim in a terminal on my MacBook Air with OS X Lion, and I can't seem to find a good plugin for Markdown syntax highlighting.

So far I've tried the plasticboy plugin and Tim Pope's plugin. The plasticboy plugin worked OK but was causing white space at the end of lines to be highlighted, and I haven't figured out how to turn that off. (It's really annoying, because every time I hit space when I'm writing it highlights the character.)

Tim's plugin didn't seem to do very much in the way of highlighting, other than maybe headers that use ###. Code blocks and bullets are ignored. I might be missing something there. I do use the .md extension on my Markdown files, so it should be picking up the filetype.

I've also seen a reference to Vim 7.3 having Markdown support built in, but without one of those two plugins I don't get any highlighting at all.

Do either of these require specific color schemes to work?

5
*.md is not the right extension for markdown. That one is for modula files. Tpope plugin works fine (all this plugins works fine) You should use the *.markdown extensionlucapette
@lucapette the ftdetect for Tim Pope's plugin does also include *.md, along with: *.markdown,*.mdown,*.mkd,*.mkdn. The plasticboy plugin auto-detects only *.mkd,*.markdown,*.mdwnpb2q
From the plasticboy plugin source, it looks like it actually does detect .md as well. I'm locked into this extension for now because I'm using Scriptogr.am for my blog, and that's the only extension they recognize right now.Josh Earl
Just found this and it's pretty rockin for github flavored markdown: github.com/jtratner/vim-flavored-markdownMatthew Turner
For issues with plasticboy/vim-markdown, please open issues on the projects so that devs can solve it. I find that highlighing trailing whitespaces is a good behavior as double spaces generate <br> tags, so writers should be made aware of that.Ciro Santilli 新疆再教育营六四事件法轮功郝海东

5 Answers

313
votes

About the native syntax highlight for markdown I think it only works for files with the extension .markdown by default.
I was having problems with markdown syntax highlight for my .md files.
I tried:

:set syntax=markdown

And it worked. So i included the following line in my .vimrc:

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

Now my vim have syntax highlight for my .md files.

BufFilePre is needed for :sav

22
votes

This should work to disable the end-of-line space highlighting when using the plasticboy mkd plugin:

:syn clear mkdLineBreak

You could autocmd that for the necessary file extensions so that you needn't do it every time you load a markdown file.

Note that this specific highlight exists because Markdown treats lines ending with 2 or more space characters specially by inserting a <br>, so it is useful.

The plasticboy plugin uses TODO highlighting for this rule, which is a bit too much as it's designed to, by default, be really garish - yellow background - so that it stands out. You can make this less visually striking by changing that highlight rule. One quick way to do this would be something like:

:hi link mkdLineBreak Underlined

Now those end-of-line spaces will show up as underlined. Try linking to other highlight groups for something that may appeal to you more. Instead of using link you can get even more specific about those end-of-line spaces: for instance you could specify that they show up as just slightly lighter/darker than the normal background, using your own highlight command, specifying custom ctermfg, ctermbg, guifg, guibg settings.

As above, you could autocmd this to apply your specific settings.

For more information about link highlight groups, type: :help group-name and you'll see a list of groups that can be linked that themselves should helpfully be displayed using their current highlight rules. Also: :help highlight.

21
votes

Native syntax highlighting

Native syntax highlighting for Markdown only works by default for the .markdown file extension.

The following line in .vimrc yields the best results for both vim and gvim:

autocmd BufNewFile,BufFilePre,BufRead *.md set filetype=markdown.pandoc

Explanation:

1. Specify your Markdown flavour!

If you work with mainly one flavour of Markdown (e.g. Pandoc), be sure to mention this as well! Doing so, allows for mixing and matching of both Markdown- and Pandoc-specific Vim plug-ins. For example: I have found the vim-pandoc-syntax plug-in particularly suitable for my highlighting needs. Nevertheless, I use the more general vim-markdown-folding for Markdown folding.

By the way, only one flavour is allowed, separated by a dot, e.g.: filetype=markdown.pandoc

2. gvim requires BufFilePre

gvim requires an additional BufFilePre in the autocommand line for Markdown file type recognition with the Save As… :sav command.

15
votes

In Tim's plugin the .md extension works only for README.md because filetype.vim specifies so.

" Markdown
au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,README.md  setf markdown
7
votes

If you don't like putting all of your configuration in ~/.vimrc, you can create ~/.vim/ftdetect/markdown.md (or its equivalent on Windows) with the following contents.

au BufNewFile,BufRead *.md setf markdown