Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active December 27, 2021 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidhq/feab2890e202b4bc25f8 to your computer and use it in GitHub Desktop.
Save davidhq/feab2890e202b4bc25f8 to your computer and use it in GitHub Desktop.
# Window toggling (switch back and forth between last two focused windows) for Sublime
# Add to Key Bindings - User:
# {
# "keys": ["f3"],
# "command": "toggle_windows"
# },
# then put toggle_windows.py (this file) into ~/Library/Application Support/Sublime Text 3/Packages/User
import os
import sublime
import sublime_plugin
windows = []
def plugin_loaded():
global windows
for window in sublime.windows():
windows.append(window.id())
windows = windows[-2:]
def workaround_bug_on_activated():
id = sublime.active_window().id()
global windows
if not windows or windows[-1] != id:
windows.append(id)
windows = windows[-2:]
def save_window_id():
workaround_bug_on_activated()
class toggle_windows_listener(sublime_plugin.EventListener):
def on_activated(self, view):
workaround_bug_on_activated()
class toggle_windows_command(sublime_plugin.WindowCommand):
def run(self):
current_window = sublime.active_window().id()
save_window_id()
switch_to = windows[-1] if windows[-1] != current_window else (windows[-2] if len(windows) > 1 else None)
if switch_to:
for window in sublime.windows():
if window.id() == switch_to:
window.bring_to_front()
break
def focus(self, window_to_move_to):
print(window_to_move_to.id())
active_view = window_to_move_to.active_view()
active_group = window_to_move_to.active_group()
# In Sublime Text 2 if a folder has no open files in it the active view
# will return None. This tries to use the actives view and falls back
# to using the active group
# Calling focus then the command then focus again is needed to make this
# work on Windows
if active_view is not None:
window_to_move_to.focus_view(active_view)
window_to_move_to.run_command('focus_neighboring_group')
window_to_move_to.focus_view(active_view)
return
if active_group is not None:
window_to_move_to.focus_group(active_group)
window_to_move_to.run_command('focus_neighboring_group')
window_to_move_to.focus_group(active_group)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment