Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to last-active tab in VIM

Tags:

vim

tabs

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.

like image 318
Paul Avatar asked Jan 22 '10 19:01

Paul


People also ask

How do I go to a previous tab in Vim?

To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast).

How do I switch between tabs in Vim?

To directly move to first tab or last tab, you can enter the following in command mode: :tabfirst or :tablast for first or last tab respectively. To move back and forth : :tabn for next tab and :tabp for previous tab. You can list all the open tabs using : :tabs. To open multiple files in tabs: $ vim -p source.

How do I open and close a tab in Vim?

You can use :tabclose (shortened alias also works :tabc ). Read more with :help tabpage . As for the buffer coming back with :bn , I believe this is normal for "hidden buffers", and :bn wraps around to the first buffer when you go past the last one. Save this answer.


3 Answers

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.

like image 191
Lucas Oman Avatar answered Oct 28 '22 12:10

Lucas Oman


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>
like image 45
Hongbo Liu Avatar answered Oct 28 '22 12:10

Hongbo Liu


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 
like image 3
ppbitb Avatar answered Oct 28 '22 12:10

ppbitb