22

I am trying to add a shortcut for "Close Others" tabs, but can't seem to find the command, here is what I am trying:

{ "keys": ["super+alt+w"], "command": "close_others" }

Cmd+Option+W - sort of like Cmd+Option+H in OS X, close all except the current tab, see?

Anyway, close_others doesn't seem to do anything. I have tried close_other_windows, close_other_tabs, nothing works. What is the right command to do that?

And while we're on it, how do you know what commands are available? My next one will be Cmd+Option+Shift+W - "Close Tabs to the Right".

For some improvements in Sublime window management see "Close all tabs, but not the window, in Sublime Text"

Thanks!

Community
  • 1
  • 1
firedev
  • 20,898
  • 20
  • 64
  • 94

2 Answers2

55

The command is close_others_by_index. Unfortunately, it takes arguments that cannot be passed via a simple key binding.

To make it work, you have to create a plugin. Tools/New Plugin...:

import sublime_plugin

class CloseOthersCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        window = self.view.window()
        group_index, view_index = window.get_view_index(self.view)
        window.run_command("close_others_by_index", { "group": group_index, "index": view_index})

Save it in Packages/User directory. Then you can add your key binding:

{ "keys": ["super+alt+w"], "command": "close_others" }

The same is true for "Close tabs to the right". The command is close_to_right_by_index.

The plugin:

import sublime_plugin

class CloseToRightCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        window = self.view.window()
        group_index, view_index = window.get_view_index(self.view)
        window.run_command("close_to_right_by_index", { "group": group_index, "index": view_index})

The key binding:

{ "keys": ["super+alt+shift+w"], "command": "close_to_right" }
Riccardo Marotti
  • 20,218
  • 7
  • 70
  • 53
  • Amazing, thank you! How do you know all this stuff? Hope this will be improved in Sublime 3. – firedev Mar 13 '13 at 10:13
  • 5
    I searched for `close_others` string on all files under `Packages` directory. The only result was `close_others_by_index` used in `Default/Tab Context.sublime-menu`. `get_view_index` is explained in [Sublime Text documentation](http://www.sublimetext.com/docs/2/api_reference.html#sublime.Window). – Riccardo Marotti Mar 13 '13 at 10:37
  • When I created a new plugin with that code, it didn't work. To fix it, I added a newline at the end of the plugin file, and I needed two empty lines between the import and class definition. This is my first time ever writing Python and a Sublime plugin; were these Python-issues or Sublime-issues? – Dan M Mar 13 '13 at 20:25
  • Those blank lines are suggested by [PEP 8](http://www.python.org/dev/peps/pep-0008/#blank-lines) style guide, but they shouldn't be mandatory. Anyway, it is a Python-issue. – Riccardo Marotti Mar 14 '13 at 08:25
  • 2
    It is really handy plugins, you should put this on github or somewhere where people can pull from. Thank you for good job! – Leonid Shagabutdinov Apr 23 '14 at 05:33
  • Working perfect on Sublime 3. Thanks! – Joaquin Iurchuk Jul 24 '16 at 06:11
  • If you want to clear **all** your tabs, you can add `{ "keys": ["super+shift+alt+w"], "command": "close_all" }` to your key bindings in Sublime 3. – diachedelic Sep 03 '20 at 23:22
10

i made a sublime-plugin for that (and for the "Close Other Windows" Functionality)

https://packagecontrol.io/packages/CloseOtherWindows

Manfred Wuits
  • 165
  • 1
  • 5