I would add a comment, but I can not format the solution.
The feedkeys solution is excellent, with a little clue, that it ALWAYS returns to normal mode, no matter what other mode you were in. I don’t want to cancel the command line mode (for dragging and dropping files on Windows) and I don’t need to cancel the visual mode, I just wanted to cancel the insert mode.
Then the solution appears as:
autocmd FocusLost * call PopOutOfInsertMode() function! PopOutOfInsertMode() if v:insertmode feedkeys("\<C-\>\<Cn>") endif endfunction
In other words, exit only if you are in insert mode. This could be clarified, since v: insertmode will be "i" in "normal insertion", "r" in "Replace" mode, and "v" in virtual replacement mode. It pops up for me independently, which is good, but the user may want to edit it.
If this does not work for you on MacVim, replace the contents of PopOutOfInsertMode with:
if v:insertmode == 'i' | call feedkeys("\<C-\>\<Cn>") | endif
dash-tom-bang
source share