Ctrl-tab to switch to most-recently used (MRU) tab

2 Points

traycerb traycerb

8 years ago

Switch back to the tab you last used (most-recently used (MRU) tab). Only works for back and forth between the current and MRU tab, so you'll need some other way to get to other tabs (e.g. the native functions or some remap).

"When triggered by leaving a tab, records current tab number (to allow switching back
"to later)
autocmd TabLeave * call RememberMRU_Tab()

"When leaving a tab, record the tab number as the MRU tab
function! RememberMRU_Tab()

    let g:MRU_Tab = tabpagenr()

"Switch to the MRU tab
function! SwitchToMRU_Tab()

    "returns the last available tab page
    let s:lastTab = tabpagenr("$")

    "Returns the current tab number
    let s:currentTab = tabpagenr()

    "only try to switch if MRU_tab isn't beyond last tab,
    "and MRU_tab isn't current tab
    if ((g:MRU_Tab <= s:lastTab) && (g:MRU_Tab != s:currentTab))
        execute "tabnext" g:MRU_Tab

"Switch to most-recently used (MRU) Tab with <Control-Tab>
nnoremap <silent> <C-tab> :call SwitchToMRU_Tab()<CR>