打开Markdown文件时Vim是否读取了html.vim?缩进配置异常排查
Hey there! Your suspicion that Vim is loading the html.vim ftplugin alongside (or after) your markdown.vim is absolutely plausible—here's how to confirm it and fix the issue.
First, Verify the Source of the Shiftwidth Setting
The quickest way to pinpoint exactly where shiftwidth=2 is coming from is to run this command while editing your .md file:
:verbose set shiftwidth?
This will output something like:
shiftwidth=2
Last set from ~/.vim/ftplugin/html.vim line 2
If it points to html.vim, your hunch is correct—Vim is loading that file after your markdown.vim, overwriting your 4-space settings.
You can also check all loaded scripts to see if html.vim is in the list:
:scriptnames
Look for entries related to html.vim and compare their line numbers to markdown.vim—later line numbers mean the script loaded after.
Another quick check: confirm your file is actually recognized as markdown:
:set filetype?
If it returns html or markdown.html, that directly explains why the HTML settings are kicking in.
Why This Happens
There are a few common triggers for this issue:
- Some popular Vim markdown plugins (like vim-markdown) configure the markdown filetype to inherit HTML settings, which can trigger the
html.vimftplugin automatically. - A misconfigured autocmd in your
vimrcor another script might be setting the filetype to HTML for.mdfiles, or loading the HTML ftplugin explicitly. - You mentioned
~/.vim/ftplug/—note that Vim's default ftplugin directory is~/.vim/ftplugin/(with an extra "in"). If that's a typo, Vim might not be loading yourmarkdown.vimat all, falling back to default or HTML settings.
Fixes to Try
Fix your ftplugin directory path
Double-check that your markdown settings are stored in~/.vim/ftplugin/markdown.vim(notftplug). Vim only looks for filetype-specific plugins in theftpluginsubdirectory by default.Force markdown settings to take priority
Ifhtml.vimloads after yourmarkdown.vim, add a buffer-local autocmd to yourmarkdown.vimto reapply your settings after all plugins finish loading:augroup MarkdownSettings autocmd! autocmd BufEnter <buffer> setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab augroup ENDThis ensures your 4-space settings are applied every time you focus the markdown buffer, overriding any later changes.
Disable HTML inheritance in your markdown plugin
If you're using a markdown plugin, check its docs for a setting to turn off HTML inheritance. For example, with vim-markdown, you can add this to yourvimrc:let g:vim_markdown_no_inherit_html = 1Check for conflicting autocmds
Run:autocmd FileType markdownto see if any autocmds are overriding your settings. If you find a conflicting rule, edit yourvimrcor the relevant script to remove it.
内容的提问来源于stack exchange,提问作者Jim

