如何让 Vim 理解 *.md 文件包含 Markdown 代码,而不是 Modula-2 代码? [英] How to make Vim understand that *.md files contain Markdown code, and not Modula-2 code?
问题描述
当我在 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 时,文件类型设置为 将以下语句添加到 ~/.vimrc 以 该语句表示当开始编辑一个不存在的新文件或开始编辑一个新缓冲区时,在将文件读入缓冲区后,如果文件匹配模式 在我现在拥有的更新版本的 Vim 中,我发现这个问题不再存在. 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 But when I edit foo.md containing Markdown code in Vim and execute 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? To understand which script was setting this filetype, I executed the following command after editing foo.md. I found the following output. In /usr/share/vim/vim74/filetype.vim, I found the following lines. These lines show that when README.md is edited, the filetype is set to Add the following statement to ~/.vimrc to 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 In the updated version of Vim that I have now, I find that this issue no longer exists. 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屋!markdown
,但在编辑扩展名为 .md 的任何其他文件时,文件类型设置为 modula2代码>.换句话说,*.md 文件被识别为 Modula-2 源代码,但 README.md 例外,因为它被识别为 Markdown 代码,这可能是因为 README.md 文件在 GitHub 上越来越受欢迎.>
解决方案
set filetype=markdown
所有 .md 文件.autocmd BufNewFile,BufRead *.md set filetype=markdown
*.md
然后 set filetype=markdown
.更新
$ 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
:set filetype?
command, I see filetype=markdown
. The Markdown syntax is highlighted correctly.:set filetype?
command, I see filetype=modula2
. The Markdown syntax is not highlighted correctly.Cause of the issue
:verbose set filetype?
filetype=modula2
Last set from /usr/share/vim/vim74/filetype.vim
au BufNewFile,BufRead *.markdown,*.mdown,*.mkd,*.mkdn,*.mdwn,README.md setf markdown
au BufNewFile,BufRead *.m2,*.DEF,*.MOD,*.md,*.mi setf modula2
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
set filetype=markdown
for all .md files.autocmd BufNewFile,BufRead *.md set filetype=markdown
*.md
then set filetype=markdown
.Update
$ 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