Sublime Forum

Close Tabs to the Left

#1

Really missing Close Tabs to the Left command :slight_smile:

0 Likes

#2

It’s trivial to add an option to the Tab Context.sublime-menu (the menu file that controls what is to be shown in the menu that opens when you right click in a tab).

import sublime
import sublime_plugin

class CloseAllTabsLeftCommand(sublime_plugin.WindowCommand):

    def run(self, group = -1, index = -1):
        left_tabs = [view for view in self.window.views_in_group(group)][0:index]
        for view in left_tabs:
            view.close()

    def is_enabled(self, group, index):
        return not (index == 0)

Save the above code in a file (you can give any name you want) as a .py (python file) in the User directory.

And the corresponding entry that needs to be added to the User/Tab Context.sublime-menu to register the command close_all_tabs_left in the menu.

    { "caption": "Close all tabs to left <-", "command": "close_all_tabs_left", "args": { "group": -1, "index": -1 }, },
2 Likes

#3

Thank you!

0 Likes

#4

Why those extra loops?

The is_enabled query is a bit odd, too, but at least is present. I like those menu items to be disabled if calling them wouldn’t have any effect. Unfortunatelly the builtins don’t do that.

Note: Named the commands to match their built-in counter parts.

import sublime_plugin


class CloseToLeftByIndexCommand(sublime_plugin.WindowCommand):

    def run(self, group = -1, index = -1):
        for view in self.window.views_in_group(group)[:index]:
            view.close()

    def is_enabled(self, group, index):
        return index > 0


class CloseUnmodifiedToLeftByIndexCommand(sublime_plugin.WindowCommand):

    def run(self, group = -1, index = -1):
        for view in self.window.views_in_group(group)[:index]:
            if not view.is_dirty():
                view.close()

    def is_enabled(self, group, index):
        return index > 0
0 Likes

#5

Thanks for presenting a much cleaner solution than mine. Yeah, I wrote them when I just started out learning plugin dev (and haven’t changed them since since they “just work”) so might be a bit eccentric.

0 Likes