[vim/vim] Markdown folding treats `#` inside fenced code blocks as headings (Issue #20764)

4 views
Skip to first unread message

VimWei

unread,
Jul 14, 2026, 1:30:57 AM (yesterday) Jul 14
to vim/vim, Subscribed
VimWei created an issue (vim/vim#20764)

Steps to reproduce

Description
When using Markdown folding in Vim (e.g., foldmethod=expr with MarkdownFold() or a plugin like vim-markdown), lines starting with # inside fenced code blocks are incorrectly recognized as Markdown headings. This causes wrong fold levels when pressing zm or zM.

Minimal reproduction

Create test.md:

test.md

Steps to reproduce

  1. Open test.md in Vim (≥ 9.1.1230).
  2. Enable Markdown folding:
    :setlocal foldmethod=expr
    :setlocal foldexpr=MarkdownFold()
  3. Press zM (close all folds).
  4. Observe that the line # This is a Python comment... creates an extra fold.

Actual behavior
Comments inside code blocks are treated as headings, producing incorrect folds.

Expected behaviour

Only genuine Markdown headings (outside code blocks) should affect folding. Lines starting with # inside fenced code blocks must be ignored.

Why this matters
The built-in helptoc package (issue #17699, fixed in 9.1.1533) already handles a similar problem correctly. The same logic should apply to Markdown folding.

Suggested fix
The foldexpr should detect whether the cursor is inside a fenced code block (by checking syntax regions or tracking fences) and skip those lines.

Version of Vim

9.2.0735

Environment

windows 10 and 11
windows terminal
gvim or terminal vim

Logs and stack traces


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/20764@github.com>

Mao-Yining

unread,
Jul 14, 2026, 3:26:32 AM (yesterday) Jul 14
to vim/vim, Subscribed
mao-yining left a comment (vim/vim#20764)

@tpope is the maintainer of vim-markdown. So I suggest you move this issue to https://github.com/tpope/vim-markdown/.


Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/20764/4966450318@github.com>

Reply all
Reply to author
Forward
0 new messages