Wikia

Vim Tips Wiki

Restore the last used mode when switching tabpages

Talk0
1,612pages on
this wiki
Revision as of 06:43, July 13, 2012 by JohnBot (Talk | contribs)

Tip 1648 Printable Monobook Previous Next

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 ~/.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

Comments

 TO DO 
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)

Around Wikia's network

Random Wiki