When writing Markdown in Vim, too many sections look messy — showing tags and folding then becomes important. This post first introduces how to define your own code folding.
au BufEnter *.md setlocal foldexpr=MarkdownLevel() au BufEnter *.md setlocal foldmethod=expr au BufEnter *.md setlocalfoldtext=MarkdownFoldText()
The MarkdownLevel function is responsible for finding fold lines and their current level.
Why does it return symbols like “>4” or “=”?
Check the documentation with : help fold
1 2 3 4 5 6 7 8 9 10 11 12 13
value meaning 0 the line is not in a fold 1, 2, the line is in a fold with this level -1 the fold level is undefined, use the fold level of a line before or after this line, whichever is the lowest. "=" use fold level from the previous line "a1", "a2", add one, two, .. to the fold level of the previous line, use the result for the current line "s1", "s2", subtract one, two, .. from the fold level of the previous line, use the result for the next line "<1", "<2", a fold with this level ends at this line ">1", ">2", a fold with this level starts at this line
Here “=” means the current line uses the same fold level as the previous line; “>1” and friends mean a
first-level fold starts at the current line, and correspondingly “>2” means a second-level fold starts here.
MarkdownFoldText defines the text shown on the single line after folding.