Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Vim understand that *.md files contain Markdown code, and not Modula-2 code? [duplicate]

Tags:

vim

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?

like image 829
Susam Pal Avatar asked Apr 24 '14 20:04

Susam Pal


People also ask

What is a .MD Markdown file?

MARKDOWN file extension. MD files are saved in plain text format that uses Markdown language which also includes inline text symbols, defining how a text can be formatted such as indentations, table formatting, fonts, and headers.

What are .MD files GitHub?

When you create a repository or a project, GitHub gives you the option of a default readme. The default readme file contains the repository name and some basic instructions. The file format is 'md', which stands for Markdown documentation. It is a lightweight markup language that can be easily converted to text.


2 Answers

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.

like image 167
Susam Pal Avatar answered Oct 20 '22 01:10

Susam Pal


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.

like image 37
Serge Stroobandt Avatar answered Oct 19 '22 23:10

Serge Stroobandt