When toggling folds with zi, any folds expanding above the current line cause the current line to disorientingly jump to a different place on the screen.
How can I keep the current line fixed in the same place while toggling folds?
You could create a mapping and use it instead of using zi. Create a mark for the current position and one for the top of the visible buffer, apply zi, move to the top, move the buffer, then move back to the original position.
nnoremap <Leader>zi maHmb'azi'bzt`a
Instead of zi
you should use zo
to open a fold and zc
to close it or za
to toggle the fold. These commands never make the current line jump for me.
Note that these commands have a recursive equivalent: zC
, zO
and zA
See
zR
and zM
seems to move the current line too so you might have to write a function which will save the position, handle the folds and restore the position back.