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.
Switch to last-active tab in VIM
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.
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 @LucasOman –
Rodolforodolph 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
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.
This should be the accepted answer. –
Aubade
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>
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
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 question –
Jodoin
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
})
© 2022 - 2024 — McMap. All rights reserved.