Sublime Text: How to jump to file from Find Result

2019-01-30 17:40发布

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....

6条回答
贼婆χ
2楼-- · 2019-01-30 18:21

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"
    }]
}
查看更多
▲ chillily
3楼-- · 2019-01-30 18:25

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

查看更多
做个烂人
4楼-- · 2019-01-30 18:31

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" }]}
查看更多
爷、活的狠高调
5楼-- · 2019-01-30 18:35

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:

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" },
查看更多
虎瘦雄心在
6楼-- · 2019-01-30 18:43

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

查看更多
Rolldiameter
7楼-- · 2019-01-30 18:44

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

查看更多
登录 后发表回答