23

I was wondering if there is a way of closing all tabs to the right of the current tab. ie

-----|active|-----|-----|------|

and after, say:

:tabcright

-----|active

Thanks! Pedro

3 Answers 3

14

No native commands for this exist, but you can create your own fairly easily using Vim script. Here is a basic example that lets you close the tabs to the right of the current tab, and the tabs to the left:

function! TabCloseRight(bang)
    let cur=tabpagenr()
    while cur < tabpagenr('$')
        exe 'tabclose' . a:bang . ' ' . (cur + 1)
    endwhile
endfunction

function! TabCloseLeft(bang)
    while tabpagenr() > 1
        exe 'tabclose' . a:bang . ' 1'
    endwhile
endfunction

command! -bang Tabcloseright call TabCloseRight('<bang>')
command! -bang Tabcloseleft call TabCloseLeft('<bang>')
0
17

Slightly improved version of davidxk's answer that works with multiple splits per tab:

:.+1,$tabdo :tabc
1
  • 1
    Wow... This answer is very short! It works thanks! Jun 9, 2021 at 7:36
7

You can use the tabdo command which allows you to run a command on a range of tabs.

:.+1,$tabdo :q

You could also put this in your vimrc so that you don't have to memorize the details of this command. You can do something like:

command -nargs=0 Tabr :.+1,$tabdo :q

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .