Help Setting Keybinding for Close Tabs to the Right


#1

I dove into the source code to find the name of the Close Tabs to the Right event, and I believe it should be:

tabs:close-tabs-to-right

However, when I add the following to my user keymap file, I am not able to successfully activate the event.

'.workspace':
  'alt-shift-r': 'tabs:close-tabs-to-right'

Are the tab events not able to be activated?


#2

Tabs commands are not registered at the workspace level but on the tab-bar, that’s why they can register a context-menu but you cannot register a keybinding.

One problem with registering this kind of command at the workspace level when in a view is that you’ll never know which view should receive the command (there’s one tab-bar per pane) except when you give the focus to the tab-bar.

My suggestion is to create you own command in your init.coffee file to trigger that event on the active tab-bar, something like:

atom.workspaceView.command 'tabs:close-tabs-to-right', ->
  atom.workspaceView.getActivePaneView().find('.tab-bar').trigger('tabs:close-tabs-to-right')

With that you can know set a keybinding for that command.


#3

Thanks for that. I see now though that the closeTabsToRight() function is literally looking for the ‘.right-clicked’ selector to decide the active tab.

So, I think I should just rewrite the closeTabsToRight function in my init file.

Thanks!


#4

Weird, I tried atom.workspaceView.getActivePaneView().find('.tab-bar').trigger('tabs:close-tabs-to-right') from a console at work and it properly removed the tabs. I guess the fact I had right-clicked in a tab prior to the console test had lead me to think it would work.

You can also put a class to the tab when triggering a command.


#5

Yeah, I had been thinking it would be bad to muck with marking things as right-clicked without actually having performed any mouse actions, but I ended up doing just that. I added the following to my init.coffee:

atom.workspaceView.command 'tabs:close-tabs-to-right', ->
  tabsBar = atom.workspaceView.getActivePaneView().find('.tab-bar')
  active = tabsBar.find('.active').addClass('right-clicked')
  tabsBar.trigger('tabs:close-tabs-to-right')
  active.removeClass('right-clicked')

Thanks for your help.


#6

I updated this to be Atom 1.0 API-compatible (it had some deprecation notices). New code:

atom.commands.add 'atom-workspace', 'custom:close-other-tabs', ->
  activePaneView = atom.views.getView(atom.workspace.getActivePane())
  tabsBar = activePaneView.querySelector('.tab-bar')
  activeTab = tabsBar.querySelector('.active')
  activeTab.classList.add('right-clicked')
  atom.commands.dispatch(tabsBar, 'tabs:close-other-tabs')
  activeTab.classList.remove('right-clicked')

#7

Oh, also I modified it to close ALL other tabs, not only the ones to the right.


#8

Good news! It appears that recent versions of Atom expose this setting directly to keymaps.

So, this now works: in your custom keymap.cson

'atom-workspace':
  'alt-shift-r': 'tabs:close-tabs-to-right'

Which is awesome! Hope that helps folks in the future. :slight_smile: