Vim Markdown Folding?

后端 未结 12 1416
梦毁少年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:36

    When I use markdown I only use the hash-style headings with space separating hashes and text. This makes the folding task a lot simpler.

    I'm pretty new to Vim, so use the following at your own risk. I added the following code to my vimrc and it folds headings based on number of hashes, and it retains the syntax colouring.

    function! MarkdownLevel()
        if getline(v:lnum) =~ '^# .*$'
            return ">1"
        endif
        if getline(v:lnum) =~ '^## .*$'
            return ">2"
        endif
        if getline(v:lnum) =~ '^### .*$'
            return ">3"
        endif
        if getline(v:lnum) =~ '^#### .*$'
            return ">4"
        endif
        if getline(v:lnum) =~ '^##### .*$'
            return ">5"
        endif
        if getline(v:lnum) =~ '^###### .*$'
            return ">6"
        endif
        return "=" 
    endfunction
    au BufEnter *.md setlocal foldexpr=MarkdownLevel()  
    au BufEnter *.md setlocal foldmethod=expr     
    

提交回复
热议问题