Skip to content

Instantly share code, notes, and snippets.

@sjl
Forked from vim-voom/markdown.vim
Created June 21, 2011 19:47
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sjl/1038710 to your computer and use it in GitHub Desktop.
Save sjl/1038710 to your computer and use it in GitHub Desktop.
Markdown folding for Vim
" folding for Markdown headers, both styles (atx- and setex-)
" http://daringfireball.net/projects/markdown/syntax#header
"
" this code can be placed in file
" $HOME/.vim/after/ftplugin/markdown.vim
func! Foldexpr_markdown(lnum)
let l1 = getline(a:lnum)
if l1 =~ '^\s*$'
" ignore empty lines
return '='
endif
let l2 = getline(a:lnum+1)
if l2 =~ '^==\+\s*'
" next line is underlined (level 1)
return '>1'
elseif l2 =~ '^--\+\s*'
" next line is underlined (level 2)
return '>2'
elseif l1 =~ '^#'
" current line starts with hashes
return '>'.matchend(l1, '^#\+')
elseif a:lnum == 1
" fold any 'preamble'
return '>1'
else
" keep previous foldlevel
return '='
endif
endfunc
setlocal foldexpr=Foldexpr_markdown(v:lnum)
setlocal foldmethod=expr
"---------- everything after this is optional -----------------------
" change the following fold options to your liking
" see ':help fold-options' for more
setlocal foldenable
setlocal foldlevel=0
setlocal foldcolumn=0
@stevepowell99
Copy link

This works great in standard vim but in gui it doesn't, any ideas?

@memeplex
Copy link

memeplex commented Aug 5, 2013

Maybe you would like to see '# hi i'm a first level header' or '## ....' etc. instead of the default fold label:

setlocal foldtext=Markdown_Foldtext()

function Markdown_Foldtext()
let l1 = getline(v:foldstart)
if l:l1[0] != '#'
return repeat('#', v:foldlevel) . ' ' . l:l1 . '...'
else
return l:l1 . '...'
fi
endfunction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment