45

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.

Paul
  • 739
  • 5
  • 11

5 Answers5

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
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 
James
  • 1,873
  • 3
  • 22
  • 28
ppbitb
  • 519
  • 1
  • 7
  • 19
  • 2
    The 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
  • 3
    AFAIK, 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, `` 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. – dylnmc Jun 21 '17 at 17:10
  • 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
})