If you are willing to dedicate a keybinding to it (let's say \G
for concreteness), then it's possible to set a mark and return to it in your keybinding.
nnoremap \G mpgg=G`p
mp
set mark. A lowercase letter means that mark is associated with the current buffer. The mark P
is universal, p
is not.
gg=G
move to the top of the buffer and indent down to the bottom, as you mentioned in the question.
`p
return to the mark.
This solution is not ideal because your cursor might still jump around because of things like scrolloff
, but it does minimize how visually distracting it is to re-indent a file fairly well in practice.