Vim Markdown Folding?

2019-01-17 03:25发布

问题:

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 how to get this working?


Alternatively, I'm using Markdown only as a way to get simple structured text. If there is a better alternative format, please also suggest. But not sure I dig TVO or VimOutliner.

回答1:

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.



回答2:

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     


回答3:

Here is a try at a recursive header folding rule. It doesn't include the underline style of Markdown header, but I'm guessing those would be awkward for your purposes anyway.

Put the following code into your .vimrc:

au FileType markdown syn region myMkdHeaderFold
        \ start="\v^\s*\z(\#{1,6})"
        \ skip="\v(\n\s*\z1\#)\@="
        \ end="\v\n(\s*\#)\@="ms=s-1,me=s-1
        \ fold contains=myMkdHeaderFold

au FileType markdown syn sync fromstart
au FileType markdown set foldmethod=syntax


回答4:

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


回答5:

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



回答6:

The only way how I get folding to work in markdown, was't very elegant, :set fdm=marker and use html comment tag

 <!-- My folding {{{1 -->

more help :help folding



回答7:

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


回答8:

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



回答9:

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


回答10:

VOoM : Vim two-pane outliner is worth checking it out.

Not only does it provide basic folding, but it also provides outline navigation via a 2nd outline view pane (similar to document map in MS Word). And it supports a large number of markup languages including others mentioned in other answers - Markdown, viki, reStructuredText, vimwiki, org, and many others.

For more info see the screenshots and the help page.



回答11:

As of Vim 8 it is included by default (via Tim Pope's markdown plugin). Just add this to .vimrc:

let g:markdown_folding=1

To make sure you have this plugin loaded you can run

:showscripts

and look for

vim80/syntax/markdown.vim


标签: vim markdown