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:

a fold collapses on paste

And here is a gif animation of my actions in Vim with default settings:

a fold collapses on paste example

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 TextChangedPrev.

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.

  • I can't reproduce your problem. Have you checked that it doesn't come from a setting in your vimrc? See How do I debug my vimrc – statox May 2 '18 at 15:55
  • @statox, I've added a gif file in order to show what happens in a more clearer way – user907860 May 4 '18 at 7:01
  • Indeed with your gif I can reproduce your problem but I can't explain it, sorry :) – statox May 4 '18 at 8:09

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.