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?
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.
TL;DR
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,
~/.vim/after/ftplugin/markdown/folding.vim
function! MarkdownFolds()
let thisline = getline(v:lnum)
if match(thisline, '^##') >= 0
return ">2"
elseif match(thisline, '^#') >= 0
return ">1"
else
return "="
endif
endfunction
setlocal foldmethod=expr
setlocal foldexpr=MarkdownFolds()
function! MarkdownFoldText()
let foldsize = (v:foldend-v:foldstart)
return getline(v:foldstart).' ('.foldsize.' lines)'
endfunction
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!
autocmd BufWinLeave ?* call <SID>make_view()
autocmd BufWinEnter ?* call <SID>load_view()
augroup END
fun! s:make_view() abort
if @% != "" && &foldmethod != 'diff'
mkview!
endif
endfun
fun! s:load_view() abort
if @% != ""
silent! loadview
endif
endfun
Disclaimer
🚨 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 🍻
Chris