created 2008 · complexity basic · author Sightless · version 7.0
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. This behavior reinforces the idea of tab pages as separate workspaces.
The following autocommands store the current mode when leaving a tab into a tab-local variable. On entry into the tab, the mode is restored. Place these autocmds in your vimrc or a .vim file in
$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
- Should probably add logic of some sort for other modes (command-line, replace, visual, etc.). Currently only properly handles insert and normal modes. –Fritzophrenic 03:59, February 14, 2011 (UTC)
- Is this the same thing? Maybe incorporate it: http://stackoverflow.com/questions/22389934/how-can-i-keep-modes-local-to-a-tab