Sublime Forum

Keyboard shortcut for "Close Other Tabs"

#1

Is there a command available for use with key bindings for the action “Close Other Tabs”?

Thanks for any insights you have!

0 Likes

#2

The command for that is close_others_by_index, but it requires you to provide the group that the file is in (the visible pane in the layout) as well as the index into that group, so that the command knows what tabs it should be closing. That makes binding the command to a key problematic because the group and index will vary.

You could use a plugin like the following, which implements a close_other_tabs command that captures the appropriate group and index and calls that command for you, though. To use it, use Tools > Developer > New Plugin... from the menu, then replace the stub with the plugin code and save it as a .py file in the location that Sublime defaults to.

import sublime
import sublime_plugin


class CloseOtherTabsCommand(sublime_plugin.TextCommand):
    """
    Tells the window to close all other views that share the same group
    in the layout as the current file.
    """
    def run(self, edit):
        group, index = self.view.window().get_view_index(self.view)
        self.view.window().run_command("close_others_by_index", {
            "group": group,
            "index": index
            })
3 Likes

#3

Holy smokes. Thanks @OdatNurd!

0 Likes

#4

close_others_by_index can directly be called with group: -1 and index: -1 to ask it to close all but the active tabs of active group in ST4.

	{
		"keys": ["ctrl+super+w"],
		"command": "close_others_by_index",
		"args": {"group": -1, "index": -1},
	},
0 Likes