Vim Markdown Folding?

后端 未结 12 1457
梦毁少年i
梦毁少年i 2020-12-08 07:10

I just realized that VIM 7.3 has built-in support for highlighting Markdown files. Excellent. However, it doesn\'t fold on the headings.

Can any offer suggestions on

12条回答
  •  清歌不尽
    2020-12-08 07:44

    Based on Jeromy & Omar's suggestions, I came up with this (for my vimrc) to automatically and unambiguously fold my DokuWiki files (in which top level header is marked by ====== at start of line, down to fourth level header marked by ===):

    function! DWTitleLevel()
        let j = len(matchstr(getline(v:lnum), '^=\+'))
        if     j =~ 6 | return ">1"
        elseif j =~ 5 | return ">2"
        elseif j =~ 4 | return ">3"
        elseif j =~ 3 | return ">4"
        endif
    endfunction
    

    '^=+' means match from the start of the line any number of contiguous '='s

    Then this in a vim modeline makes it work nicely for a DokuWiki file:

    foldmethod=expr foldexpr=DWTitleLevel() foldcolumn=5
    

    And for Markdown, I needed to write Omar's code like this:

    if empty(j) | return "=" | else | return ">".len(j) | endif
    

提交回复
热议问题