Sublime Forum

Shortcut to "Close Other Tabs"?

#1

Is there a key shortcut to execute the “Close Other Tabs” command?

0 Likes

#2

Here is the content of Packages/Default/Tab Context.sublime-menu

<menu>
	<item caption="Close Tab" command="closeUnder"/>
	<item caption="Close Other Tabs" command="closeAllExceptUnder"/>
	<item caption="Close Tabs to the Right" command="closeToRightOfUnder"/>
	<item caption="Close All" command="closeAll"/>
	<separator/>
	<item caption="Save" command="saveUnder"/>
	<separator/>
	<item caption="New view into %s" command="cloneUnder"/>
	<separator/>
	<item caption="Buffer Name..." command="editBufferNameUnder"/>
</menu>

So just add the following to your Packages/User/Default.sublime-keymap (feel free to change the key assignement :wink: )

<!--
Place your key bindings in here, this will ensure they don't get overwritten
when installing new versions of Sublime Text
-->
<bindings>
    ....
    <binding key="ctrl+alt+w"  command="closeAllExceptUnder"/>
</bindings>
1 Like

#3

The commands in the tab context menu rely on a bit of magic, and they won’t work outside of the right click context menu (as they operate on the tab under the mouse cursor, not the one that has input focus).

There isn’t a built in command that does what you want, but it is possible to make one via the python API.

1 Like

"Reveal in tree view" on tab bar
#4

OK, I’ll dig into it. If anyone has already written an extension, please let me know! :smile:

0 Likes

#5

This should do the trick:

[code]

tab_management.py

class CloseAllOtherTabsCommand(sublimeplugin.WindowCommand):
def run(self, window, args):
active_group = window.activeGroup()
curr_view_id = window.activeViewInGroup(active_group).id()

    for v in window.viewsInGroup(active_group):
        if v.id() == curr_view_id: continue
        window.focusView(v)
        window.runCommand("close")[/code]
0 Likes

#6

Thanks for the code! But, how would I take this and bind it to a key? I placed the code in a Python file and put it in the Users/Packages directory. I also changed it to extend sublimeplugin.TextCommand instead of WindowCommand.

I added the following code in my bindings:

<binding key="ctrl+t" command="closeAllOtherTabs"/>

But still no dice…

Here is the entire package that I saved in User/Packages

import sublime, sublimeplugin

# tab_management.py
class CloseAllOtherTabsCommand(sublimeplugin.TextCommand):
    def run(self, window, args):
        active_group = window.activeGroup()
        curr_view_id = window.activeViewInGroup(active_group).id()

        for v in window.viewsInGroup(active_group):
            if v.id() == curr_view_id: continue
            window.focusView(v)
            window.runCommand("close")
0 Likes

#7

It doesn’t make sense to extend TextCommand because you’re not operating on text, does it?

Also, if you do so you will get a view object passed to .run instead of a window, so the first version won’t work. You could modify the code to make it work as a TextCommand, but I see little reason to do so.

0 Likes

#8

Thanks for the response. I’m still a little unclear how to take the code you wrote and have it run when I hit a given key combination (let’s say ctrl+t). I changed it back to your original code and placed it in Packages/User. I also added the key binding shown above, but it still doesn’t close all other tabs when I press the key combination. Any ideas?

0 Likes

#9

Hm… It works here on Sublime 1.4. What version are you using?

0 Likes

#10

I’m also running 1.4. I must be making a noob mistake somewhere. I’ll try again fresh in a bit and see if I can get it working.

0 Likes

#11

Some ideas:

  1. Have you many tab_management.py files? Leave just one.

  2. Try this from the console:

import sublimeplugin
sublimeplugin.windowCommands"closeAllOtherTabs"].run(view.window(), ''])

If it works, your problem should be with the key binding. If you get a KeyError, it means Sublime can’t find the plugin.

0 Likes

#12

hey
-stumbled upon this when trying to get close all tabs keyboard shortcut and it helped alot.
-based on gullermooo’s solution and with a few syntax adjustments here is a working plugin for build 3114.

  • added extra explanations that will hopefully help newbs like me entering this amazing sublime ecosystem
  1. generate a tab_managment.py inside user directory
  2. paste this code:
    import sublime, sublime_plugin
    class close_tabs_pluginCommand(sublime_plugin.WindowCommand):
    def run(self):
    print(“running close_tabs_pluginCommand”)
    window=self.window
    active_group = window.active_group()
    curr_view_id = window.active_view_in_group(active_group).id()
    for v in window.views_in_group(active_group):
    if v.id() == curr_view_id: continue
    window.focus_view(v)
    window.run_command(“close”)
    3.make sure plugin is working when running from console:
    window.run_command(‘close_tabs_plugin’)
  3. adding keyboard shortcut: add a file called Default.sublime-keymap to same user directory, add this json:
    [{
    “keys”: [“ctrl+shift+x”],
    “command”: “close_tabs_plugin”
    }]
    thats it, hope this helps someone
    ariel
0 Likes

#13

Hey @ariel!

This works with the latest version of Sublime Text 3 (as of today that’s 3.2.1 Build 3207)!

However, I had a little bit of a hard time to copy that in my editor, so here’s your code, better formatted:

import sublime, sublime_plugin

class close_other_tabsCommand(sublime_plugin.WindowCommand):
  def run(self):
    # print("running close_other_tabsCommand")

    window = self.window
    active_group = window.active_group()
    curr_view_id = window.active_view_in_group(active_group).id()

    for v in window.views_in_group(active_group):
      if v.id() == curr_view_id: continue
      window.focus_view(v)
      window.run_command("close")

Here’s the Keybinding settings:

{ "keys": ["super+alt+w"], "command": "close_other_tabs" },
0 Likes

#14

In case anyone is interested for “close tabs to the right”, here’s the python script:

import sublime, sublime_plugin

class close_tabs_to_the_rightCommand(sublime_plugin.WindowCommand):
  def run(self):
    window = self.window
    active_group = window.active_group()
    curr_view = window.active_view()
    _groupd_idx, curr_view_idx = window.get_view_index(curr_view)

    for view in window.views_in_group(active_group):
      _group_idx, view_idx = window.get_view_index(view)
      if curr_view_idx >= view_idx: continue

      window.focus_view(view)
      window.run_command("close")

{ "keys": ["super+ctrl+w"], "command": "close_tabs_to_the_right" },

1 Like

#15
import sublime_plugin

class CloseTabsToTheRightCommand(sublime_plugin.WindowCommand):
    def get_view_idx(self, view):
        return self.window.get_view_index(view)[1]

    def run(self):
        idx_act_view = self.get_view_idx(self.window.active_view())
        for view in self.window.views_in_group(self.window.active_group()):
            if self.get_view_idx(view) > idx_act_view:
                view.close()

thanks, view.close() is undocumented and works faster because the view has not to be focused first

1 Like

#16

Here’s mine based on @makro’s code as I’ve found the view.close() to be faster.

It saves all dirty tabs and then closes all tabs to the right.

import sublime, sublime_plugin

# https://forum.sublimetext.com/t/shortcut-to-close-other-tabs/1056/16

# WHAT THIS DOES:
# Save all dirty files and close all tabs to the right.

class AllOpenFilesSaveAllCloseTabsToTheRight(sublime_plugin.WindowCommand):

  def get_view_idx(self, view):
    return self.window.get_view_index(view)[1]

  def run(self):

    window = self.window
    for view in window.views():
      if view.file_name():
        if view.is_dirty():
          view.run_command('save')

    idx_act_view = self.get_view_idx(self.window.active_view())
    for view in self.window.views_in_group(self.window.active_group()):
      if self.get_view_idx(view) > idx_act_view:
        view.close()
0 Likes

#17

@StephenS do note that there is also

window.run_command('close_to_right_by_index', {'group':0, 'index':0}) 

Which is the built in command to close tabs to the right by index.

0 Likes

#18

I did try that but it was slower.

0 Likes