I set up this mapping in my .vimrc and it works great…
" Auto indent entire file
nmap <C-f> gg=G
imap <C-f> <ESC>gg=G
However, after the operation the cursor has moved to line 1, column 1.
Is there a way to do it so that if I’m in the middle of the file somewhere the cursor will remain where it is?
Sure, use marks (
:help mark):Before executing
gg=G, the current cursor position is saved to markt. After the operation,'tjumps back to the mark.