Sublime Forum

Key Binding for Detaching a Tab to New Window or Reverse

#1

I frequently combine and split the tabs in my windows for various reasons, and I would greatly prefer using a hotkey for this instead of a mouse click-drag.

I looked through the menus and online and found nothing, I analyzed the logged commands, which was just a regular drag event, and even tried a macro which I don’t think captured the action, since I could not save it (nothing happened, so I assume the recording was empty).

Detaching a tab to a new window is straightforward, but the reverse would probably have to assume that the last focused window (excluding the current) is the merge target.

0 Likes

#2

as far as I’m aware, this functionality isn’t exposed through the API, so there is no way to get a plugin/hotkey to move a tab between windows.

0 Likes

#3

That said, you could emulate everything needed to keep the view’s state the same and just open it anew in a new window. The only thing you will lose are view-specific settings.

2 Likes

#4

I’m not so interested in moving the tab to another window, but to its own window (detachment).

0 Likes

#5

I’m not sure what you mean, how would I emulate?

0 Likes

#6

Only TextCommand commands (that is, commands that alter the state of the selection or the contents of a file) are allowed to be in macros, which is why your macro was empty.

A plugin can create a new window at will, and it can create a new view (tab) as well. So a plugin could, in response to a key binding, create a new window, create a view inside of the new window and then set that view up to be mostly identical to the original, followed by closing the original. That would mimic the tab being detached into it’s own window. This could also be done by creating the view in an already existing window and then closing the original window, which would simulate putting the tab back into another window.

The caveat is the “mostly identical” part.

The API doesn’t allow you to iterate over the list of things like settings, region keys, status line keys, etc. Instead it has a get/set/erase mentality instead. As such there are various things that you can’t fully replicate in your duplicate view because you will be unable to collect all of the information you need.

Depending on your use case, that may or may not actually matter to you, though.

2 Likes

#7

Something like this?

class MoveToNewWindowCommand(sublime_plugin.WindowCommand):
def run(self):
    tab = self.window.active_view()
    file = tab.file_name()
    if tab.is_dirty() or tab.is_scratch():
        sublime.message_dialog('You need to save your changes first!')
        return
    tab.close()
    self.window.run_command('new_window')
    new_win = sublime.active_window()
    new_win.set_sidebar_visible(True)
    new_win.open_file(file)
1 Like

#8

That may seem like a neat workaround, but you lose any settings, regions etc in the original view as well as other plugin state

0 Likes

#9

Yes definitely, that’s something that seems to be unavoidable at the moment.

I have started playing around with Sublime’s API recently and I’m putting together a plugin that, in addition to opening recent files and folders, also ‘moves’ the current tab to a new window or to a specific window. It should work only in Mac and Linux at the moment but would love some feedback if any. Here it is.

1 Like

#10

Thank you very much, @iduran!

For those not familiar with Python (as myself), the plugin above was missing some indentation and some import commands at the top. Please, see below:

import sublime
import sublime_plugin

class MoveToNewWindowCommand(sublime_plugin.WindowCommand):
	def run(self):
	    tab = self.window.active_view()
	    file = tab.file_name()
	    if tab.is_dirty() or tab.is_scratch():
	        sublime.message_dialog('You need to save your changes first!')
	        return
	    tab.close()
	    self.window.run_command('new_window')
	    new_win = sublime.active_window()
	    new_win.set_sidebar_visible(True)
	    new_win.open_file(file)

As @kingkeith pointed out, the plugin does not retain the project settings, bookmarks, selections, etc. However, it is enough for what I want.

1 Like