Switch to last-active tab in VIM
Asked Answered
V

5

46

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.

Variorum answered 22/1, 2010 at 19:8 Comment(0)
Y
72

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.

Yolande answered 22/1, 2010 at 20:10 Comment(6)
is this gvim only? mac only? I can't seem to get it working inside iterm2 on the mac (terminal vim only mode)Hymnal
For UK keyboard layout, I found this more usable nmap <c-\> :exe "tabn ".g:lasttab<CR> .. Thanks @LucasOmanRodolforodolph
I mapped it to '<Leader>tt' but this worked like a charm. Champion answer.Undemonstrative
I mapped that to t<Tab> and I love it.Viniculture
You can reduce this to just key-mappings, no extra function: nmap <c-t><c-f> :tabn<space>1<cr> nmap <c-t><c-z> :tabn<space>1<cr>:tabp<cr>Blowhole
not working in Pycharm IDE (using Vim Plugin) - this always takes you to first tab. I found using C-6 (Ctrl+6) works!Arleen
T
9

Press g<Tab> in Normal mode to go to previously visited tab. This is a standard command added in v8.2.1401. No need to make any changes to config.

Tussis answered 11/3, 2023 at 2:0 Comment(1)
This should be the accepted answer.Aubade
N
6

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>
Noctiluca answered 27/6, 2017 at 16:47 Comment(0)
S
2

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 
Springs answered 22/1, 2010 at 22:34 Comment(5)
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.Inveteracy
AFAIK, you can also use Ctrl-6 (in vimspeak it's I believe named "C-^") instead of :b#Brister
<c-^> 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, <c-^> 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 <c-^>, as that functionality relies on the register '# (@#). I presume the functionality of b # would be changed, as well, but I am not certain.Whoosh
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.Whoosh
yeah well normally people use buffers and tabs; they serve different purposes. This is not an answer to OP's questionJodoin
C
-1

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
})
Coextend answered 8/7, 2022 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.