Search code examples
firefoxvimperator

How to close tabs to the right using vimperator?


I'm using Firefox, and installed vimperator. It's great, but I can't found the method to close tabs to the right using hotkey. Could you please tell me how to do this? Thanks.


Solution

  • Close all to right/left.

    Place the following code in your .vimperatorrc file. It defines commands :closealltoright and :closealltoleft as well as bindings v> and v< respectively. Change the bindings as desired in the lines beginning "map" towards the bottom.

    js <<EOF
    
    closeAllToRight = function () {
        var current = tabs.getTab();
        var currentIx = tabs.index(current);
        var nexttab = current.nextElementSibling;
        var N = tabs.count;
        var numToClose = N - (currentIx + 1);
        tabs.remove(nexttab, numToClose);
    }
    
    closeAllToLeft = function () {
        var current = tabs.getTab();
        var currentIx = tabs.index(current);
        var firsttab = tabs.getTab(0);
        var N = tabs.count;
        var numToClose = currentIx;
        tabs.remove(firsttab, numToClose);
    }
    
    EOF
    
    " close tabs to left
    map v< :js closeAllToLeft()<CR>
    " close tabs to right
    map v> :js closeAllToRight()<CR>
    
    command! closealltoright :js closeAllToRight()
    command! closealltoleft :js closeAllToLeft()
    

    I've uploaded these two commands as a Gist.


    Pentadactyl version.

    command! closetabstoleft
        \ -description "Close all tabs to the left of the current tab"
        \ -js
        \ var firstTab = tabs.getTab(0);
        \ var numToClose = tabs.getTab().dactylOrdinal - 1;
        \ tabs.remove(firstTab, numToClose);
    command! closetabstoright
        \ -description "Close all tabs to the right of the current tab"
        \ -js
        \ tabIndex = tabs.getTab().dactylOrdinal - 1;
        \ var nextTabIndex = tabIndex + 1;
        \ var firstTab = tabs.getTab(nextTabIndex);
        \ var N = tabs.allTabs.length;
        \ var numToClose = N - nextTabIndex;
        \ tabs.remove(firstTab, numToClose);
    
    map v< -ex closetabstoleft
    map v> -ex closetabstoright
    

    I've placed these in a gist for convenience.

    Further tab commands can be found in my Pentadactyl folder (the commands and bindings are in .pentadactylrc, but may rely on functions defined in utils.js).