30

When I edit README.md containing Markdown code in Vim and execute :set filetype? command, I see filetype=markdown. The Markdown syntax is highlighted correctly.

But when I edit foo.md containing Markdown code in Vim and execute :set filetype? command, I see filetype=modula2. The Markdown syntax is not highlighted correctly.

What should I add to my ~/.vimrc to make Vim understand that foo.md or any file with extension name as .md is a markdown file and not modula2 file?

Susam Pal
  • 32,765
  • 12
  • 81
  • 103

2 Answers2

41

Cause of the issue

To understand which script was setting this filetype, I executed the following command after editing foo.md.

:verbose set filetype?

I found the following output.

  filetype=modula2
        Last set from /usr/share/vim/vim74/filetype.vim

In /usr/share/vim/vim74/filetype.vim, I found the following lines.

au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,README.md  setf markdown
au BufNewFile,BufRead *.m2,*.DEF,*.MOD,*.md,*.mi setf modula2

These lines show that when README.md is edited, the filetype is set to markdown but on editing any other file with extension name as .md, the filetype is set to modula2. In other words, *.md files are recognized as Modula-2 source code but an exception is made for README.md for it to be recognized as Markdown code, perhaps due to the growing popularity of README.md files on GitHub.

Solution

Add the following statement to ~/.vimrc to set filetype=markdown for all .md files.

autocmd BufNewFile,BufRead *.md set filetype=markdown

This statement says that when starting to edit a new file that doesn't exist or when starting to edit a new buffer, after reading the file into the buffer, if the file matches the pattern *.md then set filetype=markdown.

Update

In the updated version of Vim that I have now, I find that this issue no longer exists.

$ vim --version | head -n 2
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Mar 31 2015 23:36:07)
Included patches: 1-488, 576
$ grep -E "markdown|modula2" /usr/share/vim/vim74/filetype.vim 
au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md  setf markdown
au BufNewFile,BufRead *.m2,*.DEF,*.MOD,*.mi     setf modula2

The patch at ftp://ftp.vim.org/pub/vim/patches/7.4/7.4.860 seems to have made this change. However, I am a little confused about how these changes that seem to be available in patch 860 is available in my version of Vim which includes patches 1-448, 576 only.

Susam Pal
  • 32,765
  • 12
  • 81
  • 103
  • 1
    That `autocmd` line can also go in `~/.vim/ftdetect/markdown.vim` if you prefer a more modular configuration. – ajmccluskey May 05 '14 at 08:43
  • Somebody should tell the VIM developers to remove the .md and .mi file endings from the Modula-2 rule because these file endings are *non-standard*. The standard endings are .mod and .def (or .MOD and .DEF). The non-standard .md and .mi uses are most likely the result of some legacy code from an old code base from within the former USSR having been published on some online repo somewhere. That does not justify the use of these non-standard file endings in a tool like VIM. – trijezdci Sep 29 '15 at 14:47
  • @trijezdci This seems to have been fixed in Vim already. See the update to my answer under the section 'Update'. – Susam Pal Oct 11 '15 at 12:47
  • The [commit](https://github.com/vim/vim/commit/7d76c804) that includes this change was actually a runtime update between patch 7.4.479 and 7.4.480. I don't know, why one would leave out so many patches (I honestly don't think this makes sense), but since 7.4.576 was included you seem to have the runtimes file from that particular version as well. – Christian Brabandt Oct 11 '15 at 12:57
  • You should add the "Cause of the issue" portion as an answer on the duplicate. It's good info. – Ethan Furman Apr 05 '16 at 22:12
9

More complete answer with Markdown flavour

The other answer is correct, but incomplete. For this to equally work with the Save As… :sav command, one needs to extend the autocommand with BufFilePre:

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

It might also be interesting to specify a Markdown flavour, like Pandoc:

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

Note that Vim currently allows specifying only one flavour though.

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