What you are looking for is this :h formatexpr.
You need to define an expression that checks what mode you are in mode() ==# 'i', and then make the changes you want to make. Returning a nonzero value will use the expression expr.
eg.
set formatexpr=FormatFoo()
function! FormatFoo()
if mode() ==
echom "insertmode line wrap"
return 1
else
echom "normalmode line wrap"
return 1
endif
endfunction
source
share