如何让 Vim 理解 *.md 文件包含 Markdown 代码,而不是 Modula-2 代码? [英] How to make Vim understand that *.md files contain Markdown code, and not Modula-2 code?

查看:27
本文介绍了如何让 Vim 理解 *.md 文件包含 Markdown 代码,而不是 Modula-2 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Vim 中编辑包含 Markdown 代码的 README.md 并执行 :set filetype? 命令时,我看到 filetype=markdown.Markdown 语法正确突出显示.

但是当我在 Vim 中编辑包含 Markdown 代码的 foo.md 并执行 :set filetype? 命令时,我看到 filetype=modula2.Markdown 语法未正确突出显示.

我应该在 ~/.vimrc 中添加什么来让 Vim 理解 foo.md 或任何扩展名为 .md 的文件是 markdown 文件而不是 modula2 文件?

解决方案

问题原因

为了了解哪个脚本设置了这个文件类型,我在编辑 foo.md 后执行了以下命令.

:详细设置文件类型?

我找到了以下输出.

 filetype=modula2最后设置来自/usr/share/vim/vim74/filetype.vim

在/usr/share/vim/vim74/filetype.vim 中,我找到了以下几行.

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

这些行表明,在编辑 README.md 时,文件类型设置为 markdown,但在编辑扩展名为 .md 的任何其他文件时,文件类型设置为 modula2.换句话说,*.md 文件被识别为 Modula-2 源代码,但 README.md 例外,因为它被识别为 Markdown 代码,这可能是因为 README.md 文件在 GitHub 上越来越受欢迎.

解决方案

将以下语句添加到 ~/.vimrc 以 set filetype=markdown 所有 .md 文件.

autocmd BufNewFile,BufRead *.md set filetype=markdown

该语句表示当开始编辑一个不存在的新文件或开始编辑一个新缓冲区时,在将文件读入缓冲区后,如果文件匹配模式*.md 然后 set filetype=markdown.

更新

在我现在拥有的更新版本的 Vim 中,我发现这个问题不再存在.

$ vim --version |头-n 2VIM - Vi 改进 7.4(2013 年 8 月 10 日,2015 年 3 月 31 日 23:36:07 编译)包含的补丁:1-488、576$ grep -E "markdown|modula2"/usr/share/vim/vim74/filetype.vimau BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,*.md setf markdownau BufNewFile,BufRead *.m2,*.DEF,*.MOD,*.mi setf modula2

ftp://ftp.vim.org/pub 上的补丁/vim/patches/7.4/7.4.860 似乎做了这个改变.但是,我对补丁 860 中似乎可用的这些更改如何在我的 Vim 版本(仅包含补丁 1-448、576)中可用感到有些困惑.

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?

解决方案

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.

这篇关于如何让 Vim 理解 *.md 文件包含 Markdown 代码,而不是 Modula-2 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆