Vim Markdown Folding?

后端 未结 12 1400
梦毁少年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:41
    let g:markdown_folding = 1
    

    You can enable markdown folding feature by adding this in your .vimrc if you are using the latest version of Vim - no need to be the latest, but I don't know the exact version.

    For some reason it's not documented in the README but you can find the related code in the repository.

    FYI, if you don't want the sections closed when you open a file, refer to this SO thread. I think adding this would be the best way but you may have a different preference.

    set nofoldenable
    
    0 讨论(0)
  • 2020-12-08 07:42

    I had the same question, and played around with Jander's nice solution. The only problem is that by defining folding using syntax, you lose any Markdown syntax highlighting.

    Given that you might be interested in alternate markups, I would suggest using reStructuredText, and the amazing Vst vim extension. It does folding very nicely. Rst is much more powerful than Markdown.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-08 07:46

    There is an app a plugin for that on GitHub.

    vim-markdown-folding

    When you are editing Markdown files with Vim, you probably also want to install Tim Pope's Markdown plugin.

    vim-markdown

    0 讨论(0)
  • 2020-12-08 07:52

    There is a vim-markdown plugin at https://github.com/plasticboy/vim-markdown .

    The code related to folding from there appears to be:

    " fold region for headings
    syn region mkdHeaderFold
        \ start="^\s*\z(#\+\)"
        \ skip="^\s*\z1#\+"
        \ end="^\(\s*#\)\@="
        \ fold contains=TOP
    
    " fold region for lists
    syn region mkdListFold
        \ start="^\z(\s*\)\*\z(\s*\)"
        \ skip="^\z1 \z2\s*[^#]"
        \ end="^\(.\)\@="
        \ fold contains=TOP
    
    syn sync fromstart
    setlocal foldmethod=syntax
    
    0 讨论(0)
  • 2020-12-08 07:52

    I'm guessing you don't watch VimCasts. The guy who makes that made a pugin for just this. Here it is: https://github.com/nelstrom/vim-markdown-folding

    0 讨论(0)
提交回复
热议问题