Vim Tips Wiki

Restore the last used mode when switching tabpages

Redirected from VimTip1648

1,625pages on
this wiki
Add New Page
Talk0 Share
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 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



Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.