15

Vim properly highlights .markdown files, but it seems unfamiliar with .md filetypes. A comment on at the question https://stackoverflow.com/questions/10964681 seems to imply that .md is the wrong file extension to use for markdown files. I'm a bit confused.

Is .md the wrong extension? If not, why isn't Vim highlighting its syntax?

Vim version: 7.4

2
  • I have syntax highlighting in .md using 7.3. Could be a configuration decision. This is GNU/Linux Debian Wheezy.
    – 174140
    Jan 14 '14 at 13:00
  • 1
    "Is .md the wrong extension?" The creator of Markdown commented on this very topic recently. Jan 15 '14 at 15:00
12

Analysis

In $VIMRUNTIME/filetype.vim, you'll find this:

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

So, only README.md is detected as Markdown. One reason can be found in the same file when searching for *.md

" Modula 2
au BufNewFile,BufRead *.m2,*.DEF,*.MOD,*.md,*.mi setf modula2

Explanation

Unlike MIME types (where a registry exists), file extensions are not standardized, and (especially for short ones), multiple possible meanings overlap. It's up to you to decide how you want them handled. If it's much more probable that foo.md means Markdown file than Modula 2 source code, just override Vim's default, e.g. via

au BufNewFile,BufRead *.md  setf markdown

in ~/.vim/ftdetect/markdown.vim, or as described in :help new-filetype.

9

The

autocmd BufRead,BufNew *.md setf markdown

didn't work for me on vim 7.4 so I had to use the 'old' traditional way of setting the filetype:

autocmd BufRead,BufNew *.md set filetype=markdown

and this highlights the *.md files correctly.

1
  • 3
    This is the one you want. The top answers don't work. Nov 3 '14 at 14:39
8

Add these lines to your ~/.vimrc:

augroup markdown

    " remove previous autocmds
    autocmd!

    " set every new or read *.md buffer to use the markdown filetype 
    autocmd BufRead,BufNew *.md setf markdown

augroup END
1
  • 1
    Why re-set the filetype on each entry of the buffer? The canonical set of ftdetect events is BufNewFile,BufRead, and that should be sufficient here, too. Jan 14 '14 at 13:13
1

Try ending the file with .markdown - it may be long, but that does the trick for me.

2
  • Yeah, that's what works. However, if .md files are markdown files, I intend to make Vim play properly with them! GitHub README's uses .md as well.
    – krystah
    Jan 14 '14 at 12:57
  • You can actually use .markdown with GitHub too. Anyway, use either .md or .markdown because the others are less widely supported.
    – nyuszika7h
    Oct 13 '14 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.