kiddock's blog

Sublime Text: Open a new tab for every find & replace

Sublime Text's default behaviour is to append the results of a find operation to the currently open buffer.

I was frustrated by this for months. I'm often checking multiple related search terms across a codebase. And it seems I'm not the only one.

Be frustrated no longer. Simply copy and paste the following code and restart Sublime Text.

Sublime Text/Packages/User/UniqueFindBuffer.py

import re
import sublime
import sublime_plugin

uniqueFindViews = []

def uniqueFindUpdateViewName(view):
    contents = view.substr(sublime.Region(0, view.size()))
    contents = contents.replace("\n", "\\n")
    match = re.search("(?:\\\\n|^)Searching \d+ files for \"(.*?)\"(?:\s|\\\\n|$)", contents)
    if match:
        view.set_name("Find Results: \"" + match.group(1) + "\"")
    else:
        view.set_name("Find Results: \"\"")

class UniqueFindBufferEventListener(sublime_plugin.EventListener):
    def on_activated_async(self, view):
        if view.name() != "Find Results":
            return

        uniqueFindUpdateViewName(view)
        uniqueFindViews.append(view)

    def on_modified_async(self, view):
        if view not in uniqueFindViews:
            return

        uniqueFindUpdateViewName(view)
        uniqueFindViews.remove(view)