Sublime Forum

Fuzzy-search (& jump to) any LINE via quick-panel (plugin)

#1

lol I initially wrote this for a non-programming related project, but it’s turned out so useful, I always reach for it. I find that if I need to navigate to some place, I can almost always remember/reconstruct what the line roughly is like, so this procedure actually comes quite natural to me.

This is the code, some chunk of which is lifted out of symbol.py (seriously, if someone better versed with python and/or sublime plugin development should write a better version, preferably allowing searching all lines in all open files)

import sublime
import sublime_plugin


class NavigateToLineViaQuickPanel(sublime_plugin.WindowCommand):
    def run(self):
        v = self.window.active_view()
        osp(v)


def osp(view):

    def highlight_entry(x):
        jojo = sublime.active_window().active_view().file_name()
        window = sublime.active_window()
        window.open_file(
        jojo + ":" + str(x+1),
        group=window.active_group(),
        flags=sublime.TRANSIENT | sublime.ENCODED_POSITION | sublime.FORCE_GROUP)

    def select_entry(index, orig_view, orig_sel):
        if (index==-1):
            if orig_view:
                orig_view.sel().clear()
                orig_view.sel().add_all(orig_sel)
                orig_view.window().focus_view(orig_view)
                orig_view.show(orig_sel[0])
        else:
            view.run_command("goto_line", {"line": index+1})

    orig_sel = None
    if view:
        orig_sel = [r for r in view.sel()]

    j = view.lines(sublime.Region(0, view.size()))
    sublime.active_window().show_quick_panel(
    # [[view.substr(y),str(ind)] for ind, y in enumerate(j)],
    [view.substr(i) for i in j],
    on_select=lambda x: select_entry(x, view, orig_sel),
    on_highlight=lambda x: highlight_entry(x),
    flags=sublime.KEEP_OPEN_ON_FOCUS_LOST | sublime.MONOSPACE_FONT)
2 Likes

#2

Thanks for this plugin. I do sometimes just want to search something in the current file but I don’t actually want to move my cursor to them (I just want to see something). The only pity is that, the panel is so large that it makes most part of codes unable to be seen on my small screen :confused:


I wonder the following part won’t work in a buffer (unsaved file) since jojo will be None.

        window.open_file(
        jojo + ":" + str(x+1),
        group=window.active_group(),
        flags=sublime.TRANSIENT | sublime.ENCODED_POSITION | sublime.FORCE_GROUP)

Because this command is always working in the same file, the following command will be enough and working for a buffer as well?

self.window.active_view().run_command("goto_line", {"line": idx + 1})

Eventually, I have this, which behaves just like ctrl + r.

0 Likes

#3

Just as a heads-up, ctrl-I opens the incremental find panel which leaves you with your initial selection when you press Esc to close it (as opposed to return).

2 Likes