I have the
foldmethod=indent setting, the problem with it is that when I'm pasting a line or lines before the first line of a fold and all of these lines have the same or a greater level of indentation as the fold in question, the fold collapses.
Here is a screen shot:
And here is a gif animation of my actions in Vim with default settings:
How can I prevent this behavior? I would like the fold to remain open regardless of where I paste and what I paste.
If I paste not on the first line of a fold, or the text pasted has at least one line which has a lesser indentation level, an empty line for instance, the fold doesn't collapse.
I tried to set
foldmethod=manual before a paste, similar to a solution here, but it seems there is no such event for the autocommand, for instance something like
Currently, I suppose, that this is a bug, which has to do something with a way how Vim handles indentation folds. The folds are defined only by the number of white spaces from the "0 column".
If I paste a line with the indentation smaller, than the fold's one, the pasted line is considered to belong to a previous fold.
If I paste a line with indentation level bigger than the fold's one, then it creates a new fold, technically and should be indented (or shouldn't, since blocks smaller than two lines are not indented, as far as I know.
But if I paste a line with the same indentation as the fold's one at the fold's first line, I would suppose that line to be treated like a line in the currently, already existing fold.
And this is a bug supposedly, since it looks like Vim for some reason miscounts the indentation and thinks that a new fold has just been created, so it takes all lines above and below, which have the same indent level and collapses them.