Vim Tips Wiki
(add TipProposed for a June 2008 tip that was missed)
(→‎Comments: link to stackoverflow post on same topic)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{TipNew
{{TipProposed
 
|id=0
+
|id=1648
|previous=0
+
|previous=1647
|next=0
+
|next=1649
|created=June 30, 2008
+
|created=2008
 
|complexity=basic
 
|complexity=basic
 
|author=Sightless
 
|author=Sightless
 
|version=7.0
 
|version=7.0
 
|subpage=/201002
 
|subpage=/201002
|category1=
+
|category1=Tabs
 
|category2=
 
|category2=
 
}}
 
}}
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 <tt>~/.vim/plugin/</tt> (or <tt>$HOME/vimfiles/plugin</tt> on Windows).
+
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 <code>~/.vim/plugin/</code> (or <code>$HOME/vimfiles/plugin</code> on Windows).
 
<pre>
 
<pre>
 
augroup tabInsertMode
 
augroup tabInsertMode
Line 18: Line 20:
 
\ | let t:tabInsertMode_lastcol = col('.')
 
\ | let t:tabInsertMode_lastcol = col('.')
 
\ | if t:tabInsertMode_lastcol == col('$')
 
\ | if t:tabInsertMode_lastcol == col('$')
\ | let t:tabInsertMode_lastmode = 'A'
+
\ | let t:tabInsertMode_lastmode = 'A'
 
\ | endif
 
\ | endif
 
au TabEnter * if exists('t:tabInsertMode_lastmode')
 
au TabEnter * if exists('t:tabInsertMode_lastmode')
\ | if t:tabInsertMode_lastmode == 'A'
+
\ | if t:tabInsertMode_lastmode == 'A'
\ | if mode() == 'i' | call feedkeys("\<C-O>l")
+
\ | if mode() == 'i' | call feedkeys("\<C-O>l")
\ | else | call feedkeys("A") | endif
+
\ | else | call feedkeys("A") | endif
\ | elseif t:tabInsertMode_lastmode == 'i'
+
\ | elseif t:tabInsertMode_lastmode == 'i'
\ | if mode() != 'i' | call feedkeys("i") | endif
+
\ | if mode() != 'i' | call feedkeys("i") | endif
\ | else
+
\ | else
\ | if mode() == 'i' | call feedkeys("\<Esc>") | endif
+
\ | if mode() == 'i' | call feedkeys("\<Esc>") | endif
\ | endif
+
\ | endif
 
\ | endif
 
\ | endif
 
augroup END
 
augroup END
Line 34: Line 36:
   
 
==Comments==
 
==Comments==
  +
{{todo}}
This page was created in June 2008 and somehow missed being flagged as a "tip" so I am adding it for processing with the February 2010 new tips. [[User:JohnBeckett|JohnBeckett]] 07:15, January 25, 2011 (UTC)
 
  +
* Should probably add logic of some sort for other modes (command-line, replace, visual, etc.). Currently only properly handles insert and normal modes. &ndash;[[User:Fritzophrenic|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

Latest revision as of 15:23, 17 June 2014

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