Sublime Forum

Jump to "Find All Results" command?

#1

Is there a command that I can keybind to the “Find All Results” tab? I use this frequently (as a find all references) and need to be able to jump back and forth between file and results quickly.

0 Likes

#2

The following plugin should do what you need. Just save it in your User package. (Tools → Developer → New Plugin…)

import sublime_plugin


class FocusFindResults(sublime_plugin.WindowCommand):
    def run(self):
        candidates = [view for view in self.window.views() if view.name() == "Find Results"]
        if not candidates:
            return
        self.window.focus_view(candidates[0])
0 Likes

#3

Thanks for trying to help, I really appreciate it :slight_smile:

I’ve tried the above, oddly I’m getting the following error:

File “C:\Users\ian.wright\AppData\Roaming\Sublime Text 3\Packages\User\goto_find_results.py”, line 2, in
class FocusFindResults(sublime_plugin.WindowCommands):
AttributeError: ‘module’ object has no attribute ‘WindowCommands’

I can see however in the API reference that does appear to define a WindowCommands which is odd…

0 Likes

#4

Sorry about that. The s is extraneous and needs to be removed. I updated my post above.

0 Likes

#5

No problem - all compiles now, unfortunately I can’t get it to work. Do you know if there’s a way to easily log to the sublime console? I don’t know python or the sublime API, but I’ll have a go debugging it :smiley:

0 Likes

#6

I wrote a plugin called Find Results Buffer Utils to switch focus to and to close the Find Results buffer, and to jump to the first and last of the current results. Install with Package Control.

0 Likes

#7

Just use the Python print() function, anything printed in a plugin will be shown in the console.

0 Likes

#8

There is one last error in the code as presented above, the final line of the plugin is:

        self.window().focus_view(candidates[0])

it should be:

        self.window.focus_view(candidates[0])

With the code as originally outlined, the console would generate an error about a Window object not being callable. If you were seeing that, then making this change should fix it. If it weren’t seeing that error, then there may be an issue with how you’ve bound the focus_find_results command.

1 Like

#9

Hi Terence! Not OP but just want to say thank you for helping guys here at the forum and the great Youtube live coding sessions! Really appreciate it!

1 Like

#10

Thanks for the help - I’ve currently got the following which isn’t doing anything or printing to the console opened by control + backtick.

import sublime_plugin

class FocusFindResults(sublime_plugin.WindowCommand):
    def run(self):
        print("test")
        candidates = [view for view in self.window.views() if view.name() == "Find Results"]
        if not candidates:
            return
        self.window.focus_view(candidates[0])

Does this binding look right?

{ “keys”: [“ctrl+alt+f”], “command”: “goto_find_results” },

My file is in C:\Users\{USERNAME}\AppData\Roaming\Sublime Text 3\Packages\User\goto_find_results.py

The only messages I see in the console are:

reloading plugin User.goto_find_results
indexing [job 78]: no files were indexed out of the 1 queued, abandoning crawl

0 Likes

#11

Did you miss my post above? Scroll up 5 posts. My plugin does exactly what you want.

0 Likes

#12

No I didn’t - maybe I should use that, but I’m trying to learn a little at the same time :slight_smile:

I’m currently using a plugin called BetterFindBuffer and starting to think about trying to extend it to allow write back to the files, so any opportunity to learn a bit of how sublime plugins work is a bonus.

0 Likes

#13

You left out import sublime and used the wrong command name in your key binding.

This works:

import sublime
import sublime_plugin

class FocusFindResultsCommand(sublime_plugin.WindowCommand):
    def run(self):
        for view in self.window.views():
            if view.name() == "Find Results":
                self.window.focus_view(view)
                return
        sublime.status_message("No Find Results buffer is open")

Save the file as FocusFindResults.py anywhere in your Packages directory hierarchy. For instance in the same directory as your user Preferences.sublime-settings or key bindings are in. Or create a new directory for it, e.g. ../Your/OS/Path/To/Packages/FocusFindResults/FocusFindResults.py

Add the keys:

{ "keys": ["ctrl+alt+f"], "command": "focus_find_results" },

Command names are created by Sublime Text from the command’s class name.

  1. class FocusFindResultsCommand(sublime_plugin.WindowCommand)
  2. The Command part of the class name is removed
  3. e.g. FocusFindResultsCommand --> FocusFindResults
  4. What remains of the class name is converted to lower case snake case
  5. e.g. FocusFindResults --> focus_find_results
  6. So focus_find_results is the command name for the key binding

Sublime Text is quite forgiving and although a WindowCommand, ApplicationCommand, and TextCommand are all supposed to have Command as a suffix added to the end of the class name, in reality it can be omitted as FichteFoll did in his class in the 2nd post of this thread.

If you want to see how my plugin does the other Find Results actions have a look at the FindResultsBufferUtils.py source code.

Hope this helps.

1 Like

#14

Thanks, yeah that helped and I got it working. I had no idea that the command names were automatically generated :frowning: I’m very surprised that the little plugin I’d wrote myself previously worked given that information!

0 Likes