1

When I open a fold, either one created manually or one created automatically based off syntax, my cursor remains on the top line of the fold. Instead, I'd like it to jump to the position it was at within the fold before the fold had been closed.

I assume I could remap zc and za to create a mark at the current cursor position as a sort of PreFoldClose hook, and then I could remap zo and za to move the cursor to said mark as a sort of PostFoldOpen hook.

But I am hoping there might even be an option that simply turns this feature on for me. What do y'all think?

Bonus: how do you persist file marks across buffer loads/unloads? Are marks included by the mkview command?

1 Answer 1

1

If you close a fold from within it with zc, and then (without moving) open it again with zo, the cursor position will remain within the fold. That's because the current line number remains as it was before closing the fold, even if that line now is hidden within the fold.

However, once you move, Vim needs to update the cursor position to something that's visible. For vertical moves, that means making the first folded line the current line; all other lines are hidden within the fold, and you would have to to open the fold to go there.

If you want to change that, your suggested approach with using a mark sounds reasonable. However, that would only cover revisiting the last fold (or a maximum of 26 folds, limited by the number of available buffer-local marks). The good thing about marks is that they automatically adapt to changes in line numbers; i.e. edits above the fold would not invalidate the position information. If you wanted to store any number of folds (exceeding the number of marks), you'd have the problem that a fold can only be identified by the current range of lines, and those are bound to change during edits.

TL;DR: Go ahead and try out your idea, but be aware that it's limited to the last fold, and cannot be generalized to cover all folds.


Bonus answer: File (uppercase) marks are stored in the viminfo file, controlled by :help viminfo-f and :help viminfo-'. :mkview does not consider marks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .