Running neovim 0.1.7.

When I open a markdown file, it does not fold - even when I try zM zR zc, etc.

:set tells me ft=markdown and foldmethod=syntax

How do I get the folding?

2 Answers 2


The default Vim syntax file for markdown does not define any regions that should be folded.

If you set foldmethod in your .vimrc to syntax, then that means, syntax based folding does not work for markdown. If a plugin did set it, you might want to check with the author of the plugin, why syntax based folding does not work for you. You can check, where this was last set by :verbose :set foldmethod?

Also, you might want to consider using the plugin plasticboy/vim-markdown, as it does allow folding of markdown files, although it uses an expression-based folding method.



You're more than welcome to steal snippets from my ๐Ÿฆ‡ bat belt if you'd like ๐Ÿ‘

So I came across this same scenario a couple of days ago, and ended up watching this ๐Ÿ‘‰ ๐Ÿ“บ

Which lead me to putting the the following together,


function! MarkdownFolds()
  let thisline = getline(v:lnum)
  if match(thisline, '^##') >= 0
    return ">2"
  elseif match(thisline, '^#') >= 0
    return ">1"
    return "="
setlocal foldmethod=expr
setlocal foldexpr=MarkdownFolds()

function! MarkdownFoldText()
  let foldsize = (v:foldend-v:foldstart)
  return getline(v:foldstart).' ('.foldsize.' lines)'
setlocal foldtext=MarkdownFoldText()

but still relied on using the default key bindings for folding ๐Ÿ™ˆ

so long story short I mapped the tab key to toggle code folding with the below snippet in my .vimrc

" enable code folding
set foldmethod=syntax
" NOTE: the below settings remember code folds on a per file basis
" CREDIT: ๐Ÿฐ https://github.com/d86leader/vim-config/blob/58b5ff43c268c74c5aa2e7f6e931167f6064c02f/vimrc#L412

augroup autoview
  autocmd BufWinLeave ?* call <SID>make_view()
  autocmd BufWinEnter ?* call <SID>load_view()
augroup END
fun! s:make_view() abort
  if @% != "" && &foldmethod != 'diff'
fun! s:load_view() abort
  if @% != ""
    silent! loadview


๐Ÿšจ Make sure you create the appropriate directories to store the view files, so when you close a file / buffer (n)vim remembers to restore your previous code fold settings.

cheers ๐Ÿป

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.