Home Download Buy Blog Forum Support

Shortcut to "Close Other Tabs"?

Shortcut to "Close Other Tabs"?

Postby utotwel on Fri Dec 31, 2010 4:47 am

Is there a key shortcut to execute the "Close Other Tabs" command?
utotwel
 
Posts: 19
Joined: Fri Dec 31, 2010 4:46 am

Re: Shortcut to "Close Other Tabs"?

Postby willi297 on Fri Dec 31, 2010 6:06 pm

Here is the content of Packages/Default/Tab Context.sublime-menu
Code: Select all
<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 ;) )

Code: Select all
<!--
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>
Greg Williams
Atomic Embedded, USA
http://atomicembedded.com
willi297
 
Posts: 121
Joined: Wed Nov 18, 2009 8:29 pm
Location: Grand Rapids, MI, USA

Re: Shortcut to "Close Other Tabs"?

Postby jps on Fri Dec 31, 2010 8:09 pm

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.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Shortcut to "Close Other Tabs"?

Postby utotwel on Sat Jan 01, 2011 2:03 am

OK, I'll dig into it. If anyone has already written an extension, please let me know! :)
utotwel
 
Posts: 19
Joined: Fri Dec 31, 2010 4:46 am

Re: Shortcut to "Close Other Tabs"?

Postby guillermooo on Sat Jan 01, 2011 8:20 pm

This should do the trick:

Code: Select all
# 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")
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Shortcut to "Close Other Tabs"?

Postby utotwel on Wed Jan 05, 2011 2:23 pm

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:

Code: Select all
<binding key="ctrl+t" command="closeAllOtherTabs"/>

But still no dice...

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

Code: Select all
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")
utotwel
 
Posts: 19
Joined: Fri Dec 31, 2010 4:46 am

Re: Shortcut to "Close Other Tabs"?

Postby guillermooo on Wed Jan 05, 2011 5:25 pm

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.
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Shortcut to "Close Other Tabs"?

Postby utotwel on Wed Jan 05, 2011 7:16 pm

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?
utotwel
 
Posts: 19
Joined: Fri Dec 31, 2010 4:46 am

Re: Shortcut to "Close Other Tabs"?

Postby guillermooo on Wed Jan 05, 2011 7:53 pm

Hm... It works here on Sublime 1.4. What version are you using?
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Shortcut to "Close Other Tabs"?

Postby utotwel on Sun Jan 09, 2011 1:24 am

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.
utotwel
 
Posts: 19
Joined: Fri Dec 31, 2010 4:46 am

Next

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 23 guests