If I have a tab in insert mode and I switch to another tab that was in normal mode when I last viewed it, it's changed to insert mode. This throws me off. How can I make changing modes local to a tab?
-
4First off that isn't how it works. Secondly you should not be in insert mode for anything but short burst. It is called normal mode because it is the mode your are normally in. – Peter Rincker Mar 13 '14 at 20:41
-
It often happens when I look to other tabs for reference. Am I using vim incorrectly here? I could use windows but I have several files to reference, and I would rather keep my working file window bigger. – fent Mar 13 '14 at 21:13
-
2You are using tabs for reference, but that does not explain why you want to stay in a mode. The workflow that sounds like it would work best for you is `:set hidden` and using capital letter marks and/or ctags. I would suggest you learn how to use [buffers more effectively](http://stackoverflow.com/a/21338192/438329). – Peter Rincker Mar 13 '14 at 21:38
-
Buffers! Buffers! Buffers! Buffers! :D Tabs are basically best used for separate projects, one project per tab. This means I normally have exactly 1 tab, since I normally work on one project at a time. I also use BufExplorer to switch buffers efficiently; there are other ways to do this. – Amadan Mar 14 '14 at 02:34
-
Thanks. I've learned a lot from that link, and still learning. – fent Mar 14 '14 at 03:41
-
I suggest that tab pages are a perfectly valid thing to use in Vim. If they work for you and if you are aware of their differences compared to most other editors, there is no reason to change your habits. Although this particular request does seem somewhat strange to me, that's more for the "remaining in insert mode" than for the "using tab pages". – Ben Mar 14 '14 at 04:31
2 Answers
How do you switch to the other tab? By clicking on the tab line, with the mouse (that's the only way I could reproduce this)?! You should avoid the use of the mouse within Vim, but this will change the behavior:
:autocmd TabEnter * stopinsert
For switching tabs with the keyboard, this is usually done via the gt
command in normal mode, so you've already left insert mode (just use <Esc>
, not <C-O>
).
For more browser-like behavior, I have the following key mappings:
" CTRL-Tab next tab
noremap <C-Tab> :<C-U>tabnext<CR>
inoremap <C-Tab> <C-\><C-N>:tabnext<CR>
cnoremap <C-Tab> <C-C>:tabnext<CR>
" CTRL-SHIFT-Tab previous tab
noremap <C-S-Tab> :<C-U>tabprevious<CR>
inoremap <C-S-Tab> <C-\><C-N>:tabprevious<CR>
cnoremap <C-S-Tab> <C-C>:tabprevious<CR>

- 167,457
- 16
- 250
- 324
Vim's current mode is editor-wide. You cannot force it to follow tabs or buffers or windows or anything.
Something you CAN do, is store off the current mode when you leave a tab, and restore it when you enter the tab.
This works switching between normal and any one of the insert/replace modes; but switching among the insert/replace modes doesn't seem to work for some reason:
augroup TAB_MODES
au!
autocmd TabLeave * let t:lastmode = mode(1)
autocmd TabEnter * if !exists('t:lastmode') | let t:lastmode = 'n' | endif
autocmd TabEnter * if t:lastmode ==# 'n' | stopinsert | endif
autocmd TabEnter * if t:lastmode ==# 'i' | startinsert | endif
autocmd TabEnter * if t:lastmode ==# 'R' | startreplace | endif
autocmd TabEnter * if t:lastmode ==# 'Rv' | startgreplace | endif
augroup END
This uses a tab-local variable to store the current mode when you leave a file, and uses it to start the correct insert or replace mode when you re-enter the tab (or stop insert mode). Some modes are not included here; for example I couldn't get visual mode to work (I tried several ways of invoking gv
without success).

- 8,725
- 1
- 30
- 48