Skip to content

Instantly share code, notes, and snippets.

@mathiasrw
Created December 25, 2025 11:21
Show Gist options
  • Select an option

  • Save mathiasrw/726d8dcad2a08c7bd332b205c0ebe492 to your computer and use it in GitHub Desktop.

Select an option

Save mathiasrw/726d8dcad2a08c7bd332b205c0ebe492 to your computer and use it in GitHub Desktop.
import sublime
import sublime_plugin
import os
import time
# GLOBAL STATE
LAST_SEARCH = ""
# 1. TRACKER
class GlobalSearchTracker(sublime_plugin.EventListener):
def on_activated(self, view):
view.settings().set('gs_last_seen', time.time())
# 2. COMMAND
class GlobalSearchCommand(sublime_plugin.WindowCommand):
def run(self):
global LAST_SEARCH
# Show input panel with a trailing space and pre-selected text
input_view = self.window.show_input_panel(
"Global Search: ",
LAST_SEARCH,
self.do_search,
None,
None
)
# Select all text so you can overwrite immediately or press Enter to accept
input_view.sel().clear()
input_view.sel().add(sublime.Region(0, input_view.size()))
def do_search(self, search_term):
global LAST_SEARCH
if not search_term: return
LAST_SEARCH = search_term
matches = []
# Stable Window Sorting
all_windows = sublime.windows()
all_windows.sort(key=lambda w: w.id())
w_map = {win.id(): i + 1 for i, win in enumerate(all_windows)}
for window in all_windows:
w_rank = w_map[window.id()]
w_label = f"W{w_rank}"
for view in window.views():
if view.settings().get('is_widget'): continue
region = view.find(search_term, 0, sublime.IGNORECASE | sublime.LITERAL)
if region and region.begin() != -1:
last_seen = view.settings().get('gs_last_seen', 0.0)
name = view.file_name() or view.name() or "Untitled"
# Ensure we use standard hyphens as requested
if view.file_name():
basename = os.path.basename(name)
dirname = os.path.dirname(name)
display_text = f"{w_label} • {basename} • {dirname}"
else:
display_text = f"{w_label} • {name}"
matches.append({
'window': window,
'view': view,
'region': region,
'timestamp': last_seen,
'w_rank': w_rank,
'display_text': display_text,
'text_len': len(display_text)
})
if not matches:
sublime.message_dialog(f"No results found for: {search_term}")
return
# Sorting Logic:
# 1. Recency (Group 0) vs History (Group 1)
# 2. If Recency: Newest first
# 3. If History: Low W-number first -> Shortest Name first
def sort_key(item):
ts = item['timestamp']
group = 0 if ts > 0 else 1
if group == 0:
return (group, -ts)
else:
return (group, item['w_rank'], item['text_len'])
matches.sort(key=sort_key)
self.matches = matches
display_list = [m['display_text'] for m in matches]
# Show Quick Panel with the specific placeholder text
self.window.show_quick_panel(
display_list,
self.on_select,
flags=0,
selected_index=0,
on_highlight=None,
placeholder=f"Window and tab of documents containing '{search_term}'"
)
def on_select(self, index):
if index == -1: return
m = self.matches[index]
m['window'].bring_to_front()
m['window'].focus_view(m['view'])
m['view'].show_at_center(m['region'])
m['view'].sel().clear()
m['view'].sel().add(m['region'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment