Within a function, is there a way to determine if vim is in insert mode (or was in insert mode prior to calling the function)?
Within a function, Is there a way to determine if vim is in insert mode?
vimscript;insert mode
Something must have invoked your function. Setting the (new in Vim 8.0) asyncronous timers aside, this will be eithera custom command, which cannot be invoked from insert mode, only command-line modea mappingan :autocmd eventFor the latter, you can split into two separate :autocmds, and pass an isInsertMode flag into your function:autocmd CursorHold,CursorHoldI * call MyFunc()turns intoautocmd CursorHold  * call MyFunc(0)autocmd CursorHoldI * call MyFunc(1)For mappings, you can pass a similar flag, or a character representing the mode (to also handle visual mode and so on)::nnoremap <F2> :call MyFunc('n')<CR>:vnoremap <F2> :<C-u>call MyFunc('v')<CR>:inoremap <F2> <C-o>:call MyFunc('i')<CR>That is the canonical approach, and should work for most use cases. If you have a really special need, please elaborate in your question.