Automatic folding & unfolding
I'm running Vim 8.2 and a fresh installation of vim-markdown with the following config:
let g:vim_markdown_math = 1
let g:vim_markdown_toc_autofit = 0
let g:vim_markdown_strikethrough = 1
let g:vim_markdown_auto_insert_bullets = 0
let g:vim_markdown_new_list_item_indent = 0
let g:vim_markdown_folding_level = 1
let g:vim_markdown_override_foldtext = 0
Opening a MD file, Vim automatically folds and unfolds the entire structure. Each time I move my cursor Vim unfoldes, then, when I pause my cursor, Vim folds the structure again. This is confusing. In the following an example MD file:
# Header1
lsdkfj a;lskf ;laskdf ;laskj flaksdj f;lkasjfl;kad
# Header2
lsdkfj a;lskf ;laskdf ;laskj flaksdj f;lkasjfl;kad lsdkfj a;lskf ;laskdf ;laskj flaksdj f;lkasjfl;kad lsdkfj a;lskf ;laskdf ;laskj flaksdj f;lkasjfl;kad
# Header3
```
.
├── README.md This file.
├── xxx
│ ├── yyy
│ ├── zzz
```
For sure, if I set let g:vim_markdown_folding_disabled = 1 this issue is gone ;-)
I use let g:vim_markdown_folding_level = 6 and manually control folds with :h fold-commands.
That's how I do it too. I think I didn't describe my problem well enough :-( Vim folded and unfolded automatically during typing without my intervention. It's not about how I do fold/unfold by hand, Vim does it all by itself. You move the cursor and suddenly everything is folded. You move the cursor further and it is unfolded again. Etc.
Oh I got it. I do let g:vim_markdown_folding_disabled = 1 so that the autocmds don't to something weird and manually set foldmethod and foldexpr. So my configuration is like this:
https://github.com/tomtomjhj/init.vim/blob/4e7ce9cdfd14e638f2fe2b19896e660c65393a06/configs.vim#L375-L377
https://github.com/tomtomjhj/init.vim/blob/4e7ce9cdfd14e638f2fe2b19896e660c65393a06/ftplugin/markdown.vim#L32-L38
https://github.com/tomtomjhj/init.vim/blob/4e7ce9cdfd14e638f2fe2b19896e660c65393a06/ftplugin/markdown.vim#L64
With this configuration, folding is disabled by default and it should be initiated by running :Fold<CR>. Maybe you can register it as BufRead autocmd.
I guess the main problem of the foldexpr function is that it's not pure.
https://github.com/plasticboy/vim-markdown/blob/8e5d86f7b85234d3d1b4207dceebc43a768ed5d4/after/ftplugin/markdown.vim#L20-L45
It uses two b: variables to check if it's inside a fenced codeblock or front matter. This is broken because vim doesn't seem to guarantee that this function will be called for each line from top to bottom.
Some alternatives:
- https://github.com/tpope/vim-markdown/blob/276524ed9c8aec415d3c6dbeec7c05fbd31c95ce/ftplugin/markdown.vim#L34
- https://github.com/vim-pandoc/vim-pandoc/blob/master/autoload/pandoc/folding.vim
@tomtomjhj Thx for all those comments /and/ your time!
Re your link to tpope's vim-markdown: "Generally you don't need to install these if you are running a recent version of Vim." So you suggest to install it anyway?
Actually I like this plugin here, plasticboy/vim-markdown, and the :Toc in particular. Only the folding "automatism" is disturbing.
As I'm not used to "programming" Vim but used to the Vim's shortcuts, e.g., zf et al, what would you suggest to do and keeping the plasticboy/vim-markdown?
The thing with your first suggestion is vim_markdown_folding_style_pythonic. What is this pythonic folding?
At this moment, the only way to use plasticboy/vim-markdown and fix the folding problem is to copy-paste the folding part of tpope's plugin into your after/ftplugin/markdown.vim and modify some part of it.
This is because tpope's foldexpr function depends on the syntax definition (so does plasticboy's) to detect fenced code block. This is necessary because the code block can break folding if contains stuff that looks like markdown heading (pretty rare case, though).
Specifically, it checks if current line is markdownCode which is defined here. The problem is that plasticboy's syntax definition uses another name for code blocks.
So to fix this,
let g:vim_markdown_folding_disabled = 1- create
after/ftplugin/markdown.vimin your.vimwith following contents:- copy this code and remove lines 75,79,80.
- define function
function! s:NotCodeBlock(lnum) abort let name = synIDattr(synID(a:lnum, 1, 0), 'name') return (name =~ '^mkd\%(Code$\|Snippet\)' || name != '' && name !~ '^\%(mkd\|html\)') endfunction
What is this pythonic folding?
If I recall correctly, overall folding mechanism is similar to the default one but there are some stylistic differences.
@tomtomjhj Thx very much! I'll save this for later. Why? The weird thing is, adding pythonic, I know can't reproduce my issue:
let g:vim_markdown_math = 1
let g:vim_markdown_toc_autofit = 0
let g:vim_markdown_strikethrough = 1
let g:vim_markdown_auto_insert_bullets = 0
let g:vim_markdown_new_list_item_indent = 0
"let g:vim_markdown_conceal = 0
"let g:vim_markdown_folding_disabled = 1
let g:vim_markdown_folding_level = 6
let g:vim_markdown_folding_style_pythonic = 1
"let g:vim_markdown_override_foldtext = 0
I have the same problem, but while I add or remove content. Hope this can be solved.
Try
let g:vim_markdown_folding_disabled = 1
let g:markdown_folding = 1