Vim Tips Wiki
Advertisement

Usually when switching tabpages, Vim stays in the same mode. Some people prefer it if Vim returns to the same mode it was in when the tab page being entered was last used. These autocommands accomplish this. Place them in your vimrc or a .vim file in ~/.vim/plugin/ (or $HOME/vimfiles/plugin on Windows).

augroup tabInsertMode
au!
au TabLeave * let t:tabInsertMode_lastmode = mode()
          \ | let t:tabInsertMode_lastcol = col('.')
          \ | if t:tabInsertMode_lastcol == col('$')
          \ | let t:tabInsertMode_lastmode = 'A'
          \ | endif
au TabEnter * if exists('t:tabInsertMode_lastmode')
          \ | if t:tabInsertMode_lastmode == 'A' 
          \ |   if mode() == 'i' | call feedkeys("\<C-O>l")
          \ |   else | call feedkeys("A") | endif
          \ | elseif t:tabInsertMode_lastmode == 'i' 
          \ |   if mode() != 'i' | call feedkeys("i") | endif
          \ | else
          \ |   if mode() == 'i' | call feedkeys("\<Esc>") | endif
          \ | endif
          \ | endif
augroup END
Advertisement