151

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?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Josh Earl
  • 18,151
  • 15
  • 62
  • 91
  • 26
    `*.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` extension – lucapette Jun 09 '12 at 21:56
  • 7
    @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,*.mdwn` – pb2q Jun 09 '12 at 22:05
  • 1
    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](http://scriptogr.am) for my blog, and that's the only extension they recognize right now. – Josh Earl Jun 09 '12 at 22:34
  • 1
    Just found this and it's pretty rockin for github flavored markdown: https://github.com/jtratner/vim-flavored-markdown – Matthew Turner Nov 17 '14 at 19:54
  • 1
    For issues with [plasticboy/vim-markdown](https://github.com/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 `
    ` tags, so writers should be made aware of that.
    – Ciro Santilli OurBigBook.com Apr 24 '15 at 09:13
  • @pb2q not anymore: https://github.com/plasticboy/vim-markdown/blob/master/ftdetect/mkd.vim – Ciro Santilli OurBigBook.com Apr 24 '15 at 09:16
  • scroll down to the second answer if you just want to see how to switch on the default highlighting – JonnyRaa Sep 28 '17 at 12:13
  • @lucapette some of the biggest users of markdown are gitlab and github and they are using .md for their markdown files. – Trevor Boyd Smith Dec 13 '18 at 19:23
  • Note that some default does not handle math formulas like `$...$`. Refer to [Vim syntax and Latex math inside markdown - Stack Overflow](https://stackoverflow.com/questions/32865744/vim-syntax-and-latex-math-inside-markdown) for more details. – user202729 Sep 02 '21 at 09:27

6 Answers6

320

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

Community
  • 1
  • 1
Plínio César
  • 5,965
  • 3
  • 23
  • 30
  • 15
    For anyone else reading this: yes it works and is a much simpler solution to the problem than installing a plugin. Fast way to do this from command line: echo "au BufRead,BufNewFile *.md set filetype=markdown" >> .vimrc – RobinLovelace Oct 06 '13 at 08:45
  • 11
    It seems to me that this should be the accepted answer. – Enrico Campidoglio Feb 27 '14 at 08:55
  • IS there a bug files for vim to change this behaviour? – Sam Stoelinga Apr 27 '14 at 15:52
  • 4
    @SamStoelinga It's not a bug, `.md` is also used by Modula. Detecting which one to use would require some advanced heuristics. – alexia Oct 13 '14 at 16:03
  • 9
    As of Vim 7.4.480, `*.md` files are recognized as Markdown by default. – fwalch Feb 05 '15 at 20:55
  • @fwalch: Using Vim 7.4 as well, and the syntax highlight was not correct on `.md` files until I entered `:set syntax=markdown`. – sheac Feb 10 '15 at 20:19
  • @sheac: Do you have a version >= 7.4.480? If you have a look at https://code.google.com/p/vim/source/detail?r=31f7581068a9c3119e3bd2cd74160eb8282c3c6e, `runtime/filetype.vim` is updated to set `*.md` files to markdown. The next tagged version is 7.4.480. – fwalch Feb 11 '15 at 10:18
  • @fwalch, hmm... probably not. `:version` only gives me "7.4", but it was released in 2013, while the source you link to is from 2014. – sheac Feb 11 '15 at 17:33
  • 1
    Correct, but incomplete. For this to equally work with the `Save As… :sav` command, one needs to extend the autocommand to `BufFilePre`: `autocmd BufNewFile,BufFilePre,BufRead *.md set filetype=markdown` – Serge Stroobandt May 04 '15 at 01:13
  • 1
    Thank goodness! I'm operating inside a vagrant box, and needing to set up a vim plugin each time I `vagrant up` would be a real pain...this is MUCH simpler. Should be the accepted solution. – Wildcard Oct 17 '15 at 11:21
  • @putzkipa YTMND! – tipanverella Mar 16 '17 at 12:25
24

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.

Community
  • 1
  • 1
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
22

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.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 2
    That makes a lot of sense. Didn't realize that two spaces had significance in Markdown. I think I'll leave it enabled unless it keeps bugging me. Thanks! – Josh Earl Jun 09 '12 at 22:32
  • Yeah that's why the highlight doesn't kick in until 2 spaces have been entered. – pb2q Jun 09 '12 at 22:42
  • @josh-earl see my edits for more alternatives when using the plasticboy plugin – pb2q Jun 09 '12 at 23:13
16

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
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Spartan
  • 664
  • 9
  • 12
  • 3
    False; you must be thinking of the default Vim install, which behaves in this manner. Tim's plugin overrides it for all .md files. See https://github.com/tpope/vim-markdown/blob/master/ftdetect/markdown.vim – corvec Nov 20 '14 at 20:23
7

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

ajmccluskey
  • 505
  • 4
  • 10
0

Not sure how recent this is, but in my .vimrc this works natively, per here and https://github.com/tpope/vim-markdown ,

syntax on
filetype plugin indent on
let g:markdown_fenced_languages = ['html', 'python', 'bash=sh']

Using vim installed with brew install macvim on macOS Monterey

vim --version|head -5
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Feb 07 2023 11:35:56)
macOS version - x86_64
Included patches: 1-1276
Compiled by Homebrew
Huge version with MacVim GUI.  Features included (+) or not (-):
HeyWatchThis
  • 21,241
  • 6
  • 33
  • 41