You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

打开Markdown文件时Vim是否读取了html.vim?缩进配置异常排查

Why Your Markdown File is Using HTML's Shiftwidth Settings

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.vim ftplugin automatically.
  • A misconfigured autocmd in your vimrc or another script might be setting the filetype to HTML for .md files, 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 your markdown.vim at all, falling back to default or HTML settings.

Fixes to Try

  1. Fix your ftplugin directory path
    Double-check that your markdown settings are stored in ~/.vim/ftplugin/markdown.vim (not ftplug). Vim only looks for filetype-specific plugins in the ftplugin subdirectory by default.

  2. Force markdown settings to take priority
    If html.vim loads after your markdown.vim, add a buffer-local autocmd to your markdown.vim to reapply your settings after all plugins finish loading:

    augroup MarkdownSettings
        autocmd!
        autocmd BufEnter <buffer> setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab
    augroup END
    

    This ensures your 4-space settings are applied every time you focus the markdown buffer, overriding any later changes.

  3. 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 your vimrc:

    let g:vim_markdown_no_inherit_html = 1
    
  4. Check for conflicting autocmds
    Run :autocmd FileType markdown to see if any autocmds are overriding your settings. If you find a conflicting rule, edit your vimrc or the relevant script to remove it.

内容的提问来源于stack exchange,提问作者Jim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:59:36