In Vim, is there a way to quickly toggle between the current tab and the last-active tab? Sort of the way '' toggles between the current line and the last-active line. Plugins / keyboard mappings / voodoo all acceptable.
Asked
Active
Viewed 8,065 times
5 Answers
73
Put this in your .vimrc:
if !exists('g:lasttab')
let g:lasttab = 1
endif
nmap <Leader>tl :exe "tabn ".g:lasttab<CR>
au TabLeave * let g:lasttab = tabpagenr()
Then, in normal mode, type \tl
to swap to the tab you viewed last.

nextloop
- 166
- 9

Lucas Oman
- 15,597
- 2
- 44
- 45
-
is this gvim only? mac only? I can't seem to get it working inside iterm2 on the mac (terminal vim only mode) – Toran Billups Apr 21 '13 at 00:10
-
1For UK keyboard layout, I found this more usable `nmap
:exe "tabn ".g:lasttab – ϹοδεMεδιϲ Jun 13 '13 at 21:16` .. Thanks @LucasOman -
I mapped it to '
tt' but this worked like a charm. Champion answer. – Robert Massaioli Jun 04 '15 at 13:06 -
I mapped that to `t
` and I love it. – Ali Aref Aug 23 '21 at 05:22 -
You can reduce this to just key-mappings, no extra function: `nmap
– Toni Schilling Oct 26 '21 at 16:18:tabn 1 ` `nmap :tabn 1 :tabp ` -
not working in Pycharm IDE (using Vim Plugin) - this always takes you to first tab. I found using C-6 (Ctrl+6) works! – omsrisagar Aug 17 '23 at 15:05
7
Fix the potential issue when a tab is closed:
" Switch to last-active tab
if !exists('g:Lasttab')
let g:Lasttab = 1
let g:Lasttab_backup = 1
endif
autocmd! TabLeave * let g:Lasttab_backup = g:Lasttab | let g:Lasttab = tabpagenr()
autocmd! TabClosed * let g:Lasttab = g:Lasttab_backup
nmap <silent> <Leader>` :exe "tabn " . g:Lasttab<cr>

Hongbo Liu
- 2,818
- 1
- 24
- 18
3
I use buffers and not tabs, but I am able to switch between the current and latest used buffer using :b#
Basics of using buffers are:
:e filename to open file in new buffer
:bn to go to next buffer
:bp to go to previous buffer
:bd to close current buffer
-
2The only reason I prefer tabs over buffers is that I can reorder tabs to group files together and find things in a large session more easily. – Mike Funk Jul 20 '15 at 15:20
-
3AFAIK, you can also use Ctrl-6 (in vimspeak it's I believe named "C-^") instead of `:b#` – akavel Mar 07 '16 at 11:37
-
`
` or `ctrl` `shift` `6`, indeed, switches to last buffer. If you type `:ls` and you have at least 2 buffers open, then you will see buffer with `%` (current buffer) to left of name and another buffer with `#` (last buffer) to left of name. So, ` – dylnmc Jun 21 '17 at 17:10` effectively does `:b#`. In fact, if you `:echo @#` you will see the previous buffer you visited. Additionally, if you `let @#="foo.bar"` it will modify the behavior of ` `, as that functionality relies on the register `'#` (`@#`). I presume the functionality of `b #` would be changed, as well, but I am not certain. -
one more note: if you `:let @#="foo.bar"`, then the string `"foo.bar"` (disregarding the parentheses) must only match the path and name of a buffer that you have open. If, for example, you type `:e styles//foo.css`, then your buffer's name will be `"styles//foo.css"`, but you don't have to match the `@#` register to be `"styles//foo.css"` (although that will work). You only need to use `"styles/foo.css"`. Therefore, we can see that the buffers' names do not need to match; rather, only the preceding path names (each individually -- as if spliced on '/') and the filename need to match. – dylnmc Jun 21 '17 at 17:18
-
yeah well normally people use buffers *and* tabs; they serve different purposes. This is not an answer to OP's question – Coderino Javarino Aug 12 '19 at 12:12
1
Press g<Tab>
in Normal mode to go to previously visited tab. This is a standard command. No need to make any changes to config.

Sudhanshu
- 2,691
- 1
- 18
- 25
0
here's a solution in lua for folks that uses neovim also make sure to change <A-S-b> to your favorite keybinding.
-- switching to last active tab
vim.api.nvim_create_autocmd("TabLeave", {
pattern = "*",
callback = function()
vim.api.nvim_set_keymap('n', '<A-S-b>', '<cmd>tabn ' .. vim.api.nvim_tabpage_get_number(0) .. '<CR>', { noremap = true, silent = true })
end
})

Innocent9902
- 11
- 3