59

If you File > Find in Files... ++F you're brought to the Find Results, listing the files and highlighted matches. You can double-click either the filename/path or the matched line to open the file at the right line.

I wonder if there is a way to do exactly what the double-click does via keyboard?

With Sublimes great file switching capabilities, I thought there must be a way to keep your hands on the keyboard when doing Find in Files....

Volker E.
  • 5,911
  • 11
  • 47
  • 64
muhqu
  • 12,329
  • 6
  • 28
  • 30
  • @wdso: your edits to this question and the accepted answer add really useful details, but they would be better left as comments. :) – Jordan Gray Feb 11 '14 at 10:28

6 Answers6

65

Try Shift+F4 (fn+Shift+F4 on the Aluminum Keyboard).

Volker E.
  • 5,911
  • 11
  • 47
  • 64
Christian Lescuyer
  • 18,893
  • 5
  • 49
  • 45
  • 3
    ⇧F4 which is mapped to `prev_result` command works much better than `next_result`, however it doesn't always produce the same effect. If you are in the 'Find Results' buffer and repeatedly go to the same search result, it doesn't take you to the result underneath your cursor. It will then behave like the commands name suggest, and takes you the the previous result. While this is a minor problem I'll stick to @skuroda 's plugin, which always behaves like expected. Anyway, thanks for the answer! – muhqu Sep 30 '13 at 08:26
  • 3
    This should be the accepted answer as it does what the user is asking without requiring a plugin. Wish the default was something more intuitive than shift-F4 though. – Dan Caddigan Mar 19 '14 at 22:21
  • 6
    I am testing and with plain F4 opens next result file – Kat Lim Ruiz Jun 05 '15 at 19:54
  • 1
    @KatLimRuiz +1 didn't need *shift*. – Leo Jun 22 '16 at 11:11
  • For those who have custom key bindings the solution / key to press looks something like this: { "keys": ["super+shift+n"], "command": "next_result" }, – sean2078 Jul 04 '18 at 22:08
31

It appears a plugin has been created to do this. Took a quick look, there are some additional features in the plugin. While my original answer below will work, it will be much easier to install an existing plugin.

https://sublime.wbond.net/packages/BetterFindBuffer


Doable with a plugin.

import sublime
import sublime_plugin
import re
import os
class FindInFilesGotoCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        if view.name() == "Find Results":
            line_no = self.get_line_no()
            file_name = self.get_file()
            if line_no is not None and file_name is not None:
                file_loc = "%s:%s" % (file_name, line_no)
                view.window().open_file(file_loc, sublime.ENCODED_POSITION)
            elif file_name is not None:
                view.window().open_file(file_name)

    def get_line_no(self):
        view = self.view
        if len(view.sel()) == 1:
            line_text = view.substr(view.line(view.sel()[0]))
            match = re.match(r"\s*(\d+).+", line_text)
            if match:
                return match.group(1)
        return None

    def get_file(self):
        view = self.view
        if len(view.sel()) == 1:
            line = view.line(view.sel()[0])
            while line.begin() > 0:
                line_text = view.substr(line)
                match = re.match(r"(.+):$", line_text)
                if match:
                    if os.path.exists(match.group(1)):
                        return match.group(1)
                line = view.line(line.begin() - 1)
        return None

Set up a key binding with the command find_in_files_goto. Be careful when doing this though. Ideally, there would be some setting that identifies this view as the "Find In Files" view, so you could use that as a context. But I'm not aware of one. Of course, if you do find one, let me know.

Edit Pulling up the example key binding into the main body of the answer.

{
    "keys": ["enter"],
    "command": "find_in_files_goto",
    "context": [{
        "key": "selector",
        "operator": "equal",
        "operand": "text.find-in-files"
    }]
}
Raine Revere
  • 30,985
  • 5
  • 40
  • 52
skuroda
  • 19,514
  • 4
  • 50
  • 34
  • Yay! ..works like a charm! \o/ I don't use the "Find In Files" view... always use it with "Use Buffer" option. – muhqu May 29 '13 at 11:42
  • Sorry for not being clear. Just pointing out that a user keybinding to perform the goto action will also try to run in a normal file without any context. Ideally, you could limit a binding to just to the "Find Results" view. This would then allow you to reuse that keybinding elsewhere. Also I meant the "buffer" option when I have been saying view anyways. :) – skuroda May 29 '13 at 23:19
  • 2
    Oh... of course you can restrict the key-binding to only apply to the "Find Results" view. You can check for the scope `text.find-in-files`. e.g. `{ "keys": ["enter"], "command": "find_in_files_goto", "context": [{"key": "selector", "operator": "equal", "operand": "text.find-in-files" }]}` – muhqu May 30 '13 at 10:10
  • 1
    Hmm, why did looking at the scope not occur to me. Oh well :). I know you can check the name of the view, but that wouldn't really allow you to easily reuse the keybinding for other things. Anyways, glad it helped you solve your problem! – skuroda May 30 '13 at 15:53
  • so good! thank you.. robarson's comment is a great help too.. `{ "keys": ["enter"], "command": "find_in_files_goto", "context": [{"key": "selector", "operator": "equal", "operand": "text.find-in-files" }]}` – RichardJohnn Sep 18 '13 at 17:01
  • Learning Python, one Sublime Plugin at a time! – Dogoku Feb 11 '14 at 17:16
  • It seems there's a regular plugin now (based on this answer), named [BetterFindBuffer](https://sublime.wbond.net/packages/BetterFindBuffer), installable via Package Control. – akavel Dec 25 '14 at 11:45
  • 3
    Great plugin. Any chance someone themed it to Monokai? – MikeHall Oct 20 '15 at 22:31
22

on SublimeText 3 I had to use F4(for going to the current result file) and Shift +F4 (for previous result).

From the default keymap...

{ "keys": ["super+shift+f"], "command": "show_panel", "args": {"panel": "find_in_files"} },
{ "keys": ["f4"], "command": "next_result" },
{ "keys": ["shift+f4"], "command": "prev_result" },

I hope this post helps.

SP

Som Poddar
  • 1,428
  • 1
  • 15
  • 22
10

the command 'next_result' will do this. using the neat idea muhqu posted about using scope, you can make it so that you can press 'enter' on the line that you want to goto:

,{ "keys": ["enter"], "command": "next_result", "context": [{"key": "selector", 
"operator": "equal", "operand": "text.find-in-files" }]}
robarson
  • 433
  • 4
  • 12
  • 1
    Just tried it out, but it doesn't work exactly as expected. The 'next_result' command iterates over the full list of search results and doesn't open the search result that is currently focused on. e.g. have a search result with 3 results and move cursor to the 3rd result line, hit 'enter' and it will still bring you to the first result... For me skuroda's script works like a charm. Anyway, thanks for the answer! – muhqu Jul 04 '13 at 09:14
  • this was helpful in combination with skuroda's plugin – RichardJohnn Sep 18 '13 at 17:03
  • @muhqu is correct. This doesn't do what you would expect it to do. It will open up the _next_ result instead of the _current_ result your cursor is currently on. Very confusing. You need to create a plugin to do this properly, as can be seen here: https://stackoverflow.com/a/41567906/293280 – Joshua Pinter Dec 27 '19 at 16:54
5

try Ctrl+P - this quick-opens files by name in your project, For a full list of keyboard shortcuts see here

3

If you are on Sublime Text build 4153 or newer:

A new command called current_result was added for this purpose: https://github.com/sublimehq/sublime_text/issues/6014#issuecomment-1669019483


If you are on Sublime Text build 4143 or older:

It is possible to emulate a double click in Sublime Text by executing the drag_select command with an argument of "by": "words" (as seen in the Default sublime-mousemap file).

However, you need to pretend that the mouse is where the caret is for this work. The following plugin will do this (Tools menu -> Developer -> New Plugin..., and replace the template with the following):

import sublime
import sublime_plugin


class DoubleClickAtCaretCommand(sublime_plugin.TextCommand):
    def run(self, edit, **kwargs):
        view = self.view
        window_offset = view.window_to_layout((0,0))
        vectors = []
        for sel in view.sel():
            vector = view.text_to_layout(sel.begin())
            vectors.append((vector[0] - window_offset[0], vector[1] - window_offset[1]))
        for idx, vector in enumerate(vectors):
            view.run_command('drag_select', { 'event': { 'button': 1, 'count': 2, 'x': vector[0], 'y': vector[1] }, 'by': 'words', 'additive': idx > 0 or kwargs.get('additive', False) })

To be used in combination with a keybinding like:

{ "keys": ["alt+/"], "command": "double_click_at_caret" },
Keith Hall
  • 15,362
  • 3
  • 53
  • 71
  • Works like a charm!!! Much better than just using the `next_result` command. I did add `"context": [ {"key": "selector", "operator": "equal", "operand": "text.find-in-files" } ]` to my key binding so it is only bound in the Find Results tab, but not sure if it's necessary. – Joshua Pinter Dec 27 '19 at 16:51
  • Just coming back here to re-set this up after upgrading Sublime Text. To be a little more helpful, you can create a Plugin by going `Tools` -> `Developer` -> `New Plugin..`. And the final keybinding (in my case) was: `{ "keys": ["g", "o"], "command": "double_click_at_caret", "context": [ {"key": "selector", "operator": "equal", "operand": "text.find-in-files" } ] }`. This binds "g" and "o" (sequentially) to open up the search results and will only work in the search results tab so it doesn't pollute the rest of the keybindings, etc. – Joshua Pinter Nov 11 '22 at 19:00
  • 1
    This doesn't work on latest version of sublime (Build 4147) anymore, I came up with a different approach after some digging, hope it helps: https://gist.github.com/chendesheng/99e673571c8dd19ddf9557a5ff6d2bcc – chendesheng Jan 09 '23 at 14:26
  • @chendesheng THANK YOU FOR UPDATING THIS SCRIPT FOR THE LATEST BUILD OF SUBLIME TEXT! This did the trick! – Joshua Pinter Aug 11 '23 at 14:36