Q:

vim: how to map gg=G in insert mode

Can someone tell me how to map gg=G in insert mode.  When I use map <F2> <C-O>gg=G the cursor is correctly set to the beginning of the file but I then get =G inserted.  I did try gg\=G which also did not work.  This is for running MacVim on High Sierra.  

A:

inoremap <F2> <ESC>gg=G

This creates a non-recursive insertion-mode remapping of <F2> to <ESC>gg=G. The <ESC> is used to move from insert mode into normal mode for gg=G.
Note that in order to use function keys as a mapping, you may need to add this additional line to your .vimrc, from this question:
set <F2>=<C-v><F2>

