As others have pointed out in the question comments, this is a somewhat complex issue that I really DON'T have the expertise to expound on. That being said, the crux of the trouble I was having was related to automatic saving/loading of views (which include info on folds) when a buffer is quit/loaded.
To automatically save/load folds in vim, one adds something like the below to one's vimrc
.
augroup auto_save_folds
autocmd!
au BufWinLeave ?* mkview
au BufWinEnter ?* silent loadview
augroup end
All the above code does is to automatically save folds when the buffer is unloaded, and automatically load the folds when the buffer is loaded. The way it does this, is via info stored in the views directory. With the above code included in the vimrc
, when one looks in the ~/.vim/view
(can be different in different OS'es), one sees a number of files, with filenames similar to (but interspersed with special characters throughout the name) actual files one has ever opened in vim.
Normally, the above works. But when using splits, it is a slightly different story. Each split is considered a different view, albeit of the same buffer. So when one has the same file in splits, and makes changes that affect the folds in one of the splits, then vim has no way of reconciling the changes in one split with the rest of the splits - so the information on the 'views' (folds et al) in each of the splits keeps overwriting each other. Because all of the splits use the same view to store their own info.
What needs to be done is to give the program a way to better organize this information. That's done by giving names to each of the views. Assuming a use case of 2 views, the code above would look like -
augroup auto_save_folds
autocmd!
au BufWinLeave ?* mkview 1
au BufWinEnter ?* silent loadview 1
au BufWinLeave ?* mkview 2
au BufWinEnter ?* silent loadview 2
.
.
augroup end
I often have up to 4-5 views, so the above numbering goes on till 5.
With this setup, when one again looks in the view
directory (after opening and folding a fresh file), one can see that for each file opened in vim, there are now 2 files (), appropriately suffixed in the view
directory. This way the folds in the individual splits do not overwrite each other.
So far, this has worked nicely for me, using manual folds (which I continue to stick with because of personal preferences). But, given the complex interplay of various issues here, I cannot assure that this will work for all.
foldmethod
? – Matt Sep 6 '19 at 11:49mkview/loadview
? The generated name depends on the buffer name, so with the splits they will overwrite one another. You need to supply a number, likemkview 1 / loadview 1
,mkview 2 / loadview 2
etc. – Matt Sep 6 '19 at 12:38BufWinEnter
norBufWinLeave
are triggered when a buffer is split into several windows. Will that be okay if I propose to OP to use automatic folds, or save the manual folds manually, or usemksession
withssop+=folds
? I'm still unsure what he's up to. – Matt Sep 8 '19 at 10:31